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