Mercurial > hg > octave-lyh
annotate scripts/strings/strvcat.m @ 8442:502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
author | Thorsten Meyer <thorsten.meyier@gmx.de> |
---|---|
date | Mon, 05 Jan 2009 08:11:03 +0100 |
parents | 8dff9cba15fe |
children |
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}) | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
21 ## Return a character array containing the strings (or cell-strings) |
5820 | 22 ## @var{s_1}, @dots{}, @var{s_n} as |
23 ## its rows. Each string is padded with blanks in order to form a valid | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
24 ## matrix. For numerical input, each element is converted to the |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
25 ## corresponding ASCII character. Unlike @var{char}, empty strings are |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
26 ## removed. For example: |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
27 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
28 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
29 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
30 ## strvcat ([97, 98, 99], "", @{"98", "99", 100@}, ["num", "bers"]) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
31 ## @result{} ans = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
32 ## ["abc " |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
33 ## "98 " |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
34 ## "99 " |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
35 ## "d " |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
36 ## "numbers"] |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
37 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
38 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
39 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8372
diff
changeset
|
40 ## @seealso{char, cstrcat, strcat} |
5820 | 41 ## @end deftypefn |
42 | |
43 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> | |
44 ## Adapted-By: jwe | |
45 ## Modified: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> converted | |
46 ## str2mat to strvcat. Same function except that strvcat | |
47 ## ignores empty strings. | |
48 ## Modified by Alois Schloegl <a.schloegl@ieee.org> Mar 2005 | |
49 ## added support for cell-strings | |
50 ## Modifed by David Bateman for NDArrays | |
51 | |
52 function retval = strvcat (varargin) | |
53 | |
54 if (nargin == 0) | |
6046 | 55 print_usage (); |
5820 | 56 endif |
57 | |
58 nr = zeros (nargin, 1); | |
59 nc = zeros (nargin, 1); | |
60 K = 0; | |
7208 | 61 nd = ndims (varargin{1}); |
62 sz = size (varargin{1}); | |
63 for k = 1:nargin | |
5820 | 64 s = varargin{k}; |
65 if (iscell (s)) | |
66 for k1 = 1:length(s) | |
7208 | 67 K++; |
5820 | 68 nr(K) = size (s{k1}, 1); |
69 nc(K) = size (s{k1}, 2); | |
70 if (ndims (s{k1}) != nd) | |
71 error ("strvcat: dimension mismatch"); | |
72 else | |
73 if (any (sz(3:nd) != size (s{k1}) (3:nd))) | |
74 error ("strvcat: dimension mismatch"); | |
75 endif | |
76 endif | |
77 endfor | |
78 else | |
7208 | 79 K++; |
5820 | 80 nr(K) = size (s, 1); |
81 nc(K) = size (s, 2); | |
82 if (ndims (s) != nd) | |
83 error ("strvcat: dimension mismatch"); | |
84 else | |
85 if (any (sz(3:nd) != size (s) (3:nd))) | |
86 error ("strvcat: dimension mismatch"); | |
87 endif | |
88 endif | |
89 endif | |
90 endfor | |
91 | |
92 sz(1) = sum (nr); | |
93 sz(2) = max (nc); | |
94 retval = char (ones (sz) * toascii (" ")); | |
95 | |
96 idx = cell(nd,1); | |
7208 | 97 for k = 3:nd |
98 idx{k} = sz{k}; | |
5820 | 99 endfor |
100 | |
101 K = 0; | |
102 row_offset = 0; | |
7208 | 103 for k = 1:nargin |
5820 | 104 s = varargin{k}; |
105 if (iscell (s)) | |
106 for k1 = 1:length(s) | |
107 K = K + 1; | |
108 idx{1} = [row_offset + 1 : row_offset + nr(k)]; | |
109 idx{2} = [1 : nc(K)]; | |
110 retval(idx{:}) = char(s{k1}); | |
111 row_offset = row_offset + size (s{k1}, 1); | |
112 endfor | |
113 else | |
7208 | 114 K++; |
5820 | 115 if (nc(K) > 0) |
7208 | 116 retval ((row_offset+1):(row_offset+nr(K)), 1:nc(K)) = char (s); |
5820 | 117 endif |
118 row_offset = row_offset + nr(K); | |
119 endif | |
120 endfor | |
121 | |
122 endfunction | |
123 | |
124 %!shared s1,s2,s3,s4,c | |
125 %! s1 = "quick"; s2 = "brown"; s3 = "fox"; s4 = ["quick";"brown";"fox "]; | |
126 %! c{1} = s1; c{2} = s2; c{3} = s3; | |
127 %!assert(strvcat(s1,s2,s3),s4) | |
128 %!assert(strvcat(c),s4) | |
129 %!assert(strvcat(s4,s4),[s4;s4]); |