time | calls | line |
---|
| | 1 | function t = strcat(varargin)
|
| | 2 | %STRCAT Concatenate strings.
|
| | 3 | % COMBINEDSTR = STRCAT(S1, S2, ..., SN) horizontally concatenates strings
|
| | 4 | % in arrays S1, S2, ..., SN. Inputs can be combinations of single
|
| | 5 | % strings, strings in scalar cells, character arrays with the same number
|
| | 6 | % of rows, and same-sized cell arrays of strings. If any input is a cell
|
| | 7 | % array, COMBINEDSTR is a cell array. Otherwise, COMBINEDSTR is a
|
| | 8 | % character array.
|
| | 9 | %
|
| | 10 | % Notes:
|
| | 11 | %
|
| | 12 | % For character array inputs, STRCAT removes trailing ASCII white-space
|
| | 13 | % characters: space, tab, vertical tab, newline, carriage return, and
|
| | 14 | % form-feed. To preserve trailing spaces when concatenating character
|
| | 15 | % arrays, use horizontal array concatenation, [s1, s2, ..., sN].
|
| | 16 | %
|
| | 17 | % For cell array inputs, STRCAT does not remove trailing white space.
|
| | 18 | %
|
| | 19 | % When combining nonscalar cell arrays and multi-row character arrays,
|
| | 20 | % cell arrays must be column vectors with the same number of rows as the
|
| | 21 | % character arrays.
|
| | 22 | %
|
| | 23 | % Example:
|
| | 24 | %
|
| | 25 | % strcat({'Red','Yellow'},{'Green','Blue'})
|
| | 26 | %
|
| | 27 | % returns
|
| | 28 | %
|
| | 29 | % 'RedGreen' 'YellowBlue'
|
| | 30 | %
|
| | 31 | % See also CAT, CELLSTR.
|
| | 32 |
|
| | 33 | % Copyright 1984-2009 The MathWorks, Inc.
|
| | 34 |
|
| | 35 | % The cell array implementation is in @cell/strcat.m
|
| | 36 |
|
| 2 | 37 | narginchk(1, inf);
|
| | 38 |
|
| | 39 | % initialise return arguments
|
| 2 | 40 | t = '';
|
| | 41 |
|
| | 42 | % get number of rows of each input
|
| 2 | 43 | rows = cellfun('size',varargin,1);
|
| | 44 | % get number of dimensions of each input
|
| 2 | 45 | twod = (cellfun('ndims',varargin) == 2);
|
| | 46 |
|
| | 47 | % return empty string when all inputs are empty
|
| 2 | 48 | if all(rows == 0)
|
| | 49 | return;
|
| | 50 | end
|
| 2 | 51 | if ~all(twod)
|
| | 52 | error(message('MATLAB:strfun:InputDimension'));
|
| | 53 | end
|
| | 54 |
|
| | 55 | % Remove empty inputs
|
| 2 | 56 | k = (rows == 0);
|
| 2 | 57 | varargin(k) = [];
|
| 2 | 58 | rows(k) = [];
|
| 2 | 59 | maxrows = max(rows);
|
| | 60 | % Scalar expansion
|
| | 61 |
|
| 2 | 62 | for i=1:length(varargin),
|
| 4 | 63 | if rows(i)==1 && rows(i)<maxrows
|
| | 64 | varargin{i} = varargin{i}(ones(1,maxrows),:);
|
| | 65 | rows(i) = maxrows;
|
| | 66 | end
|
| 4 | 67 | end
|
| | 68 |
|
| 2 | 69 | if any(rows~=rows(1)),
|
| | 70 | error(message('MATLAB:strcat:NumberOfInputRows'));
|
| | 71 | end
|
| | 72 |
|
| 2 | 73 | n = rows(1);
|
| 2 | 74 | space = sum(cellfun('prodofsize',varargin));
|
| 2 | 75 | s0 = blanks(space);
|
0.01 | 2 | 76 | scell = cell(1,n);
|
| 2 | 77 | notempty = true(1,n);
|
| 2 | 78 | s = '';
|
| 2 | 79 | for i = 1:n
|
| 2 | 80 | s = s0;
|
| 2 | 81 | str = varargin{1}(i,:);
|
| 2 | 82 | if ~isempty(str) && (str(end) == 0 || isspace(str(end)))
|
| | 83 | str = char(deblank(str));
|
| | 84 | end
|
| 2 | 85 | pos = length(str);
|
| 2 | 86 | s(1:pos) = str;
|
| 2 | 87 | pos = pos + 1;
|
| 2 | 88 | for j = 2:length(varargin)
|
| 2 | 89 | str = varargin{j}(i,:);
|
| 2 | 90 | if ~isempty(str) && (str(end) == 0 || isspace(str(end)))
|
| | 91 | str = char(deblank(str));
|
| | 92 | end
|
| 2 | 93 | len = length(str);
|
| 2 | 94 | s(pos:pos+len-1) = str;
|
| 2 | 95 | pos = pos + len;
|
| 2 | 96 | end
|
| 2 | 97 | s = s(1:pos-1);
|
| 2 | 98 | notempty(1,i) = ~isempty(s);
|
| 2 | 99 | scell{1,i} = s;
|
| 2 | 100 | end
|
| 2 | 101 | if n > 1
|
| | 102 | t = char(scell{notempty});
|
| 2 | 103 | else
|
| 2 | 104 | t = s;
|
| 2 | 105 | end
|