Mercurial > hg > octave-lyh
annotate scripts/strings/strvcat.m @ 7815:a41df65f3f00
Add some single precision test code and fix resulting bugs
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 28 May 2008 01:03:35 +0200 |
parents | 3422f39573b1 |
children | 8dff9cba15fe |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1996, 2006, 2007 Kurt Hornik |
5820 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5820 | 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/>. | |
5820 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} strvcat (@var{s_1}, @dots{}, @var{s_n}) | |
21 ## Return a matrix containing the strings (and cell-strings) | |
22 ## @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. Unlike @var{str2mat}, empty strings are ignored. | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7208
diff
changeset
|
25 ## @seealso{cstrcat, str2mat} |
5820 | 26 ## @end deftypefn |
27 | |
28 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> | |
29 ## Adapted-By: jwe | |
30 ## Modified: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> converted | |
31 ## str2mat to strvcat. Same function except that strvcat | |
32 ## ignores empty strings. | |
33 ## Modified by Alois Schloegl <a.schloegl@ieee.org> Mar 2005 | |
34 ## added support for cell-strings | |
35 ## Modifed by David Bateman for NDArrays | |
36 | |
37 function retval = strvcat (varargin) | |
38 | |
39 if (nargin == 0) | |
6046 | 40 print_usage (); |
5820 | 41 endif |
42 | |
43 nr = zeros (nargin, 1); | |
44 nc = zeros (nargin, 1); | |
45 K = 0; | |
7208 | 46 nd = ndims (varargin{1}); |
47 sz = size (varargin{1}); | |
48 for k = 1:nargin | |
5820 | 49 s = varargin{k}; |
50 if (iscell (s)) | |
51 for k1 = 1:length(s) | |
7208 | 52 K++; |
5820 | 53 nr(K) = size (s{k1}, 1); |
54 nc(K) = size (s{k1}, 2); | |
55 if (ndims (s{k1}) != nd) | |
56 error ("strvcat: dimension mismatch"); | |
57 else | |
58 if (any (sz(3:nd) != size (s{k1}) (3:nd))) | |
59 error ("strvcat: dimension mismatch"); | |
60 endif | |
61 endif | |
62 endfor | |
63 else | |
7208 | 64 K++; |
5820 | 65 nr(K) = size (s, 1); |
66 nc(K) = size (s, 2); | |
67 if (ndims (s) != nd) | |
68 error ("strvcat: dimension mismatch"); | |
69 else | |
70 if (any (sz(3:nd) != size (s) (3:nd))) | |
71 error ("strvcat: dimension mismatch"); | |
72 endif | |
73 endif | |
74 endif | |
75 endfor | |
76 | |
77 sz(1) = sum (nr); | |
78 sz(2) = max (nc); | |
79 retval = char (ones (sz) * toascii (" ")); | |
80 | |
81 idx = cell(nd,1); | |
7208 | 82 for k = 3:nd |
83 idx{k} = sz{k}; | |
5820 | 84 endfor |
85 | |
86 K = 0; | |
87 row_offset = 0; | |
7208 | 88 for k = 1:nargin |
5820 | 89 s = varargin{k}; |
90 if (iscell (s)) | |
91 for k1 = 1:length(s) | |
92 K = K + 1; | |
93 idx{1} = [row_offset + 1 : row_offset + nr(k)]; | |
94 idx{2} = [1 : nc(K)]; | |
95 retval(idx{:}) = char(s{k1}); | |
96 row_offset = row_offset + size (s{k1}, 1); | |
97 endfor | |
98 else | |
7208 | 99 K++; |
5820 | 100 if (nc(K) > 0) |
7208 | 101 retval ((row_offset+1):(row_offset+nr(K)), 1:nc(K)) = char (s); |
5820 | 102 endif |
103 row_offset = row_offset + nr(K); | |
104 endif | |
105 endfor | |
106 | |
107 endfunction | |
108 | |
109 %!shared s1,s2,s3,s4,c | |
110 %! s1 = "quick"; s2 = "brown"; s3 = "fox"; s4 = ["quick";"brown";"fox "]; | |
111 %! c{1} = s1; c{2} = s2; c{3} = s3; | |
112 %!assert(strvcat(s1,s2,s3),s4) | |
113 %!assert(strvcat(c),s4) | |
114 %!assert(strvcat(s4,s4),[s4;s4]); |