5820
|
1 ## Copyright (C) 1996 Kurt Hornik |
|
2 ## |
|
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 |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} strvcat (@var{s_1}, @dots{}, @var{s_n}) |
|
22 ## Return a matrix containing the strings (and cell-strings) |
|
23 ## @var{s_1}, @dots{}, @var{s_n} as |
|
24 ## its rows. Each string is padded with blanks in order to form a valid |
|
25 ## matrix. Unlike @var{str2mat}, empty strings are ignored. |
|
26 ## @seealso{strcat, str2mat} |
|
27 ## @end deftypefn |
|
28 |
|
29 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> |
|
30 ## Adapted-By: jwe |
|
31 ## Modified: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> converted |
|
32 ## str2mat to strvcat. Same function except that strvcat |
|
33 ## ignores empty strings. |
|
34 ## Modified by Alois Schloegl <a.schloegl@ieee.org> Mar 2005 |
|
35 ## added support for cell-strings |
|
36 ## Modifed by David Bateman for NDArrays |
|
37 |
|
38 function retval = strvcat (varargin) |
|
39 |
|
40 if (nargin == 0) |
6046
|
41 print_usage (); |
5820
|
42 endif |
|
43 |
|
44 nr = zeros (nargin, 1); |
|
45 nc = zeros (nargin, 1); |
|
46 K = 0; |
|
47 nd = ndims (varargin {1}); |
|
48 sz = size (varargin {1}); |
|
49 for k = 1 : nargin |
|
50 s = varargin{k}; |
|
51 if (iscell (s)) |
|
52 for k1 = 1:length(s) |
|
53 K = K+1; |
|
54 nr(K) = size (s{k1}, 1); |
|
55 nc(K) = size (s{k1}, 2); |
|
56 if (ndims (s{k1}) != nd) |
|
57 error ("strvcat: dimension mismatch"); |
|
58 else |
|
59 if (any (sz(3:nd) != size (s{k1}) (3:nd))) |
|
60 error ("strvcat: dimension mismatch"); |
|
61 endif |
|
62 endif |
|
63 endfor |
|
64 else |
|
65 K = K + 1; |
|
66 nr(K) = size (s, 1); |
|
67 nc(K) = size (s, 2); |
|
68 if (ndims (s) != nd) |
|
69 error ("strvcat: dimension mismatch"); |
|
70 else |
|
71 if (any (sz(3:nd) != size (s) (3:nd))) |
|
72 error ("strvcat: dimension mismatch"); |
|
73 endif |
|
74 endif |
|
75 endif |
|
76 endfor |
|
77 |
|
78 sz(1) = sum (nr); |
|
79 sz(2) = max (nc); |
|
80 retval = char (ones (sz) * toascii (" ")); |
|
81 |
|
82 idx = cell(nd,1); |
|
83 for k = 3 : nd; |
|
84 idx {k} = sz {k}; |
|
85 endfor |
|
86 |
|
87 K = 0; |
|
88 row_offset = 0; |
|
89 for k = 1 : nargin |
|
90 s = varargin{k}; |
|
91 if (iscell (s)) |
|
92 for k1 = 1:length(s) |
|
93 K = K + 1; |
|
94 idx{1} = [row_offset + 1 : row_offset + nr(k)]; |
|
95 idx{2} = [1 : nc(K)]; |
|
96 retval(idx{:}) = char(s{k1}); |
|
97 row_offset = row_offset + size (s{k1}, 1); |
|
98 endfor |
|
99 else |
|
100 K = K + 1; |
|
101 if (nc(K) > 0) |
|
102 retval ((row_offset + 1) : (row_offset + nr(K)), 1:nc(K)) = char(s); |
|
103 endif |
|
104 row_offset = row_offset + nr(K); |
|
105 endif |
|
106 endfor |
|
107 |
|
108 endfunction |
|
109 |
|
110 %!shared s1,s2,s3,s4,c |
|
111 %! s1 = "quick"; s2 = "brown"; s3 = "fox"; s4 = ["quick";"brown";"fox "]; |
|
112 %! c{1} = s1; c{2} = s2; c{3} = s3; |
|
113 %!assert(strvcat(s1,s2,s3),s4) |
|
114 %!assert(strvcat(c),s4) |
|
115 %!assert(strvcat(s4,s4),[s4;s4]); |