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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2273
|
19 |
3361
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} str2mat (@var{s_1}, @dots{}, @var{s_n}) |
|
22 ## Return a matrix containing the strings @var{s_1}, @dots{}, @var{s_n} as |
|
23 ## its rows. Each string is padded with blanks in order to form a valid |
|
24 ## matrix. |
3426
|
25 ## |
3361
|
26 ## This function is modelled after @sc{Matlab}. In Octave, you can create |
|
27 ## a matrix of strings by @code{[@var{s_1}; @dots{}; @var{s_n}]} even if |
|
28 ## the strings are not all the same length. |
|
29 ## @end deftypefn |
2311
|
30 |
5428
|
31 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> |
2355
|
32 ## Adapted-By: jwe |
2314
|
33 |
3979
|
34 function retval = str2mat (varargin) |
2325
|
35 |
2273
|
36 if (nargin == 0) |
|
37 usage ("str2mat (s1, ...)"); |
|
38 endif |
|
39 |
|
40 nc = 0; |
3168
|
41 nr = 0; |
2273
|
42 |
3168
|
43 nr = zeros (nargin, 1); |
|
44 nc = zeros (nargin, 1); |
2273
|
45 for k = 1 : nargin |
3979
|
46 s = varargin{k}; |
3705
|
47 if (! isstr (s)) |
|
48 s = setstr (s); |
2273
|
49 endif |
3705
|
50 [nr(k), nc(k)] = size (s); |
2273
|
51 endfor |
|
52 |
3174
|
53 tmp = find (nr == 0); |
|
54 |
|
55 if (! isempty (tmp)) |
|
56 nr(tmp) = 1; |
|
57 endif |
|
58 |
3168
|
59 retval_nr = sum (nr); |
|
60 retval_nc = max (nc); |
|
61 |
|
62 retval = setstr (ones (retval_nr, retval_nc) * toascii (" ")); |
2273
|
63 |
3168
|
64 row_offset = 0; |
2273
|
65 for k = 1 : nargin |
3979
|
66 s = varargin{k}; |
3705
|
67 if (! isstr (s)) |
|
68 s = setstr (s); |
|
69 endif |
3168
|
70 if (nc(k) > 0) |
|
71 retval ((row_offset + 1) : (row_offset + nr(k)), 1:nc(k)) = s; |
2273
|
72 endif |
3168
|
73 row_offset = row_offset + nr(k); |
2273
|
74 endfor |
2325
|
75 |
2273
|
76 endfunction |