2313
|
1 ## Copyright (C) 1996 Kurt Hornik |
2325
|
2 ## |
2313
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
2313
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2273
|
18 |
3361
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} str2mat (@var{s_1}, @dots{}, @var{s_n}) |
|
21 ## Return a matrix containing the strings @var{s_1}, @dots{}, @var{s_n} as |
|
22 ## its rows. Each string is padded with blanks in order to form a valid |
|
23 ## matrix. |
3426
|
24 ## |
3361
|
25 ## This function is modelled after @sc{Matlab}. In Octave, you can create |
|
26 ## a matrix of strings by @code{[@var{s_1}; @dots{}; @var{s_n}]} even if |
|
27 ## the strings are not all the same length. |
|
28 ## @end deftypefn |
2311
|
29 |
5428
|
30 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
31 ## Adapted-By: jwe |
2314
|
32 |
3979
|
33 function retval = str2mat (varargin) |
2325
|
34 |
2273
|
35 if (nargin == 0) |
6046
|
36 print_usage (); |
2273
|
37 endif |
|
38 |
|
39 nc = 0; |
3168
|
40 nr = 0; |
2273
|
41 |
3168
|
42 nr = zeros (nargin, 1); |
|
43 nc = zeros (nargin, 1); |
2273
|
44 for k = 1 : nargin |
3979
|
45 s = varargin{k}; |
5443
|
46 if (! ischar (s)) |
|
47 s = char (s); |
2273
|
48 endif |
3705
|
49 [nr(k), nc(k)] = size (s); |
2273
|
50 endfor |
|
51 |
3174
|
52 tmp = find (nr == 0); |
|
53 |
|
54 if (! isempty (tmp)) |
|
55 nr(tmp) = 1; |
|
56 endif |
|
57 |
3168
|
58 retval_nr = sum (nr); |
|
59 retval_nc = max (nc); |
|
60 |
5443
|
61 retval = char (ones (retval_nr, retval_nc) * toascii (" ")); |
2273
|
62 |
3168
|
63 row_offset = 0; |
2273
|
64 for k = 1 : nargin |
3979
|
65 s = varargin{k}; |
5443
|
66 if (! ischar (s)) |
|
67 s = char (s); |
3705
|
68 endif |
3168
|
69 if (nc(k) > 0) |
|
70 retval ((row_offset + 1) : (row_offset + nr(k)), 1:nc(k)) = s; |
2273
|
71 endif |
3168
|
72 row_offset = row_offset + nr(k); |
2273
|
73 endfor |
2325
|
74 |
2273
|
75 endfunction |