annotate scripts/strings/strjoin.m @ 16400:424463a80134

New function strjoin.m * scripts/strings/strjoin.m: New function (Introduced in Matlab R2013a).
author Ben Abbott <bpabbott@mac.com>
date Sun, 31 Mar 2013 10:36:56 -0400
parents
children c6d61dca5acd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16400
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
1 ## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@uta.edu>
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
2 ## Copyright (C) 2013 Ben Abbott <bpabbott@mac.com>
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
3 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
4 ## This file is part of Octave.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
5 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ## your option) any later version.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
10 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ## General Public License for more details.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
15 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
18 ## <http://www.gnu.org/licenses/>.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## -*- texinfo -*-
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
21 ## @deftypefn {Function File} {@var{str} =} strjoin (@var{cstr})
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
22 ## @deftypefnx {Function File} {@var{str} =} strjoin (@var{cstr}, @var{delimiter})
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
23 ## Joins the elements of the cell-string array, @var{cstr}, into a single
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
24 ## string.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
25 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
26 ## If no @var{delimiter} is specified, the elements of @var{cstr}
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
27 ## seperated by a space.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
28 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
29 ## If @var{delimiter} is specified as a string, the cell-string array is
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
30 ## joined using the string.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
31 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
32 ## If @var{delimiter} is a cell-string array whose length is one less
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
33 ## than @var{cstr}, then the elemennts of @var{cstr} are joined by
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34 ## interleaving the cell-string elements of @var{delimiter}.
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 ##
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36 ## @example
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 ## @group
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
38 ## strjoin (@{'Octave','Scilab','Lush','Yorick'@}, '*')
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39 ## @result{} 'Octave*Scilab*Lush*Yorick'
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40 ## @end group
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
41 ## @end example
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
42 ## @seealso {strsplit}
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 ## @end deftypefn
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45 function rval = strjoin (cstr, delimiter)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 if (nargin == 1)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48 delimiter = " ";
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49 elseif (nargin < 1 || nargin > 2)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 print_usage ();
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51 elseif (! (iscellstr (cstr) && (ischar (delimiter) || iscellstr (delimiter))))
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52 print_usage ();
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 endif
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55 if (numel (cstr) == 1)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 rval = cstr{1};
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 return
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 endif
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
60 if (ischar (delimiter))
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
61 delimiter = {delimiter};
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
62 end
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64 num = numel (cstr);
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 if (numel (delimiter) == 1 && num > 1)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 delimiter = repmat (delimiter, 1, num);
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67 delimiter(end) = {""};
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
68 elseif (numel (delimiter) != num - 1)
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69 error ("strjoin:cellstring_delimiter_mismatch",
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
70 "strjoin: the number of delimiters does not match the number of strings")
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 else
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72 delimiter(end+1) = {""};
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 endif
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
74
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 rval = sprintf ("%s", [cstr(:).'; delimiter(:).']{:});
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 endfunction
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79 %!assert (strjoin ({"hello"}, "-"), "hello")
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 %!assert (strjoin ({"hello", "world"}), "hello world")
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
81 %!assert (strjoin ({"Octave", "Scilab", "Lush", "Yorick"}, "*"),
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 %! "Octave*Scilab*Lush*Yorick")
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83 %!assert (strjoin ({"space", "comma", "dash", "semicolon", "done"},
424463a80134 New function strjoin.m
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
84 %! {" ", ",", "-", ";"}), "space comma,dash-semicolon;done")