Mercurial > hg > octave-nkf
annotate scripts/strings/strcat.m @ 10509:ddbd812d09aa
properly compress sparse matrices after assembly
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Apr 2010 12:57:44 +0200 |
parents | e60f038146e1 |
children | 693e22af08ae |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, |
8920 | 2 ## 2005, 2006, 2007, 2008, 2009 John W. Eaton |
10032 | 3 ## Copyright (C) 2009 Jaroslav Hajek |
2313 | 4 ## |
5 ## This file is part of Octave. | |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
2313 | 11 ## |
12 ## Octave is distributed in the hope that it will be useful, but | |
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 ## General Public License for more details. | |
16 ## | |
17 ## You should have received a copy of the GNU General Public License | |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
920 | 20 |
3361 | 21 ## -*- texinfo -*- |
22 ## @deftypefn {Function File} {} strcat (@var{s1}, @var{s2}, @dots{}) | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
23 ## Return a string containing all the arguments concatenated |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
24 ## horizontally. If the arguments are cells strings, @code{strcat} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
25 ## returns a cell string with the individual cells concatenated. |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
26 ## For numerical input, each element is converted to the |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## corresponding ASCII character. Trailing white space is eliminated. |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
28 ## For example, |
3426 | 29 ## |
3361 | 30 ## @example |
31 ## @group | |
32 ## s = [ "ab"; "cde" ]; | |
33 ## strcat (s, s, s) | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
34 ## @result{} ans = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
35 ## "ab ab ab " |
3361 | 36 ## "cdecdecde" |
37 ## @end group | |
38 ## @end example | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
39 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
40 ## @example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
41 ## @group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
42 ## s = @{ "ab"; "cde" @}; |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
43 ## strcat (s, s, s) |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
44 ## @result{} ans = |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
45 ## @{ |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
46 ## [1,1] = ababab |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
47 ## [2,1] = cdecdecde |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
48 ## @} |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
49 ## @end group |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
50 ## @end example |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
51 ## |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8143
diff
changeset
|
52 ## @seealso{cstrcat, char, strvcat} |
3361 | 53 ## @end deftypefn |
2697 | 54 |
2314 | 55 ## Author: jwe |
56 | |
6138 | 57 function st = strcat (varargin) |
920 | 58 |
7504
ddcf233d765b
detect cellstr args in strcat
John W. Eaton <jwe@octave.org>
parents:
7411
diff
changeset
|
59 if (nargin > 0) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
60 if (nargin == 1) |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
61 st = varargin{1}; |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
62 elseif (nargin > 1) |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
63 ## Convert to cells of strings |
10032 | 64 uo = "UniformOutput"; |
65 reals = cellfun (@isreal, varargin); | |
66 if (any (reals)) | |
67 varargin(reals) = cellfun (@char, varargin(reals), uo, false); | |
68 endif | |
69 chars = cellfun (@ischar, varargin); | |
70 allchar = all (chars); | |
71 varargin(chars) = cellfun (@cellstr, varargin(chars), uo, false); | |
72 if (! all (cellfun (@iscell, varargin))) | |
73 error ("strcat: inputs must be strings or cells of strings"); | |
74 endif | |
7504
ddcf233d765b
detect cellstr args in strcat
John W. Eaton <jwe@octave.org>
parents:
7411
diff
changeset
|
75 |
10037
e60f038146e1
further simplify strcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10032
diff
changeset
|
76 ## We don't actually need to bring all cells to common size, because |
e60f038146e1
further simplify strcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10032
diff
changeset
|
77 ## cellfun can now expand scalar cells. |
e60f038146e1
further simplify strcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10032
diff
changeset
|
78 err = common_size (varargin{:}); |
10032 | 79 |
80 if (err) | |
81 error ("strcat: arguments must be the same size, or be scalars"); | |
82 endif | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
83 |
10037
e60f038146e1
further simplify strcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10032
diff
changeset
|
84 ## Cellfun handles everything for us. |
e60f038146e1
further simplify strcat
Jaroslav Hajek <highegg@gmail.com>
parents:
10032
diff
changeset
|
85 st = cellfun (@horzcat, varargin{:}, uo, false); |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
86 |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
87 if (allchar) |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
88 ## If all inputs were strings, return strings. |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
89 st = char (st); |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
90 endif |
7504
ddcf233d765b
detect cellstr args in strcat
John W. Eaton <jwe@octave.org>
parents:
7411
diff
changeset
|
91 endif |
ddcf233d765b
detect cellstr args in strcat
John W. Eaton <jwe@octave.org>
parents:
7411
diff
changeset
|
92 else |
6046 | 93 print_usage (); |
920 | 94 endif |
95 | |
7504
ddcf233d765b
detect cellstr args in strcat
John W. Eaton <jwe@octave.org>
parents:
7411
diff
changeset
|
96 endfunction |
920 | 97 |
6138 | 98 ## test the dimensionality |
99 ## 1d | |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
100 %!assert(strcat("ab ", "ab "), "abab") |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
101 %!assert(strcat({"ab "}, "ab "), {"ab ab"}) |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
102 %!assert(strcat("ab ", {"ab "}), {"abab "}) |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
103 %!assert(strcat({"ab "}, {"ab "}), {"ab ab "}) |
7813 | 104 %!assert(strcat("", "ab"), "ab") |
105 %!assert(strcat("", {"ab"}, {""}), {"ab"}) | |
6138 | 106 ## 2d |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
107 %!assert(strcat(["ab ";"cde"], ["ab ";"cde"]), ["abab ";"cdecde"]) |
7411 | 108 |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
109 ## test for deblanking implied trailing spaces of character input |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
110 %!assert((strcmp (strcat ("foo", "bar"), "foobar") && |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
111 %! strcmp (strcat (["a"; "bb"], ["foo"; "bar"]), ["afoo "; "bbbar"]))); |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
112 |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
113 ## test for mixing character and cell inputs |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
114 %!assert(all (strcmp (strcat ("a", {"bc", "de"}, "f"), {"abcf", "adef"}))) |
7411 | 115 |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
116 ## test for scalar strings with vector strings |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
117 %!assert(all (strcmp (strcat (["a"; "b"], "c"), ["ac"; "bc"]))) |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
118 |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
119 ## test with cells with strings of differing lengths |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
120 %!assert(all (strcmp (strcat ({"a", "bb"}, "ccc"), {"accc", "bbccc"}))) |
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7504
diff
changeset
|
121 %!assert(all (strcmp (strcat ("a", {"bb", "ccc"}), {"abb", "accc"}))) |
7504
ddcf233d765b
detect cellstr args in strcat
John W. Eaton <jwe@octave.org>
parents:
7411
diff
changeset
|
122 |
7411 | 123 %!error strcat (); |
124 | |
8143
3a4694d67dbb
strcat.m: Compatibility of non-char data
Ben Abbott <bpabbott@mac.com>
parents:
7813
diff
changeset
|
125 %!assert (strcat (1, 2), strcat (char(1), char(2))) |
7411 | 126 |
8143
3a4694d67dbb
strcat.m: Compatibility of non-char data
Ben Abbott <bpabbott@mac.com>
parents:
7813
diff
changeset
|
127 %!assert (strcat ('', 2), strcat ([], char(2))) |
3a4694d67dbb
strcat.m: Compatibility of non-char data
Ben Abbott <bpabbott@mac.com>
parents:
7813
diff
changeset
|
128 |