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 | 5 | 41 | narginchk(1, inf);
|
| | 42 |
|
< 0.001 | 5 | 43 | for i = 1:nargin
|
< 0.001 | 10 | 44 | input = varargin{i};
|
< 0.001 | 10 | 45 | if ~isnumeric(input) && ~ischar(input) && ~iscell(input)
|
| | 46 | error(message('MATLAB:strcat:InvalidInputType'));
|
| | 47 | end
|
< 0.001 | 10 | 48 | end
|
| | 49 |
|
| | 50 | % Initialize return arguments
|
< 0.001 | 5 | 51 | t = '';
|
| | 52 |
|
| | 53 | % Get number of rows of each input
|
< 0.001 | 5 | 54 | rows = cellfun('size', varargin, 1);
|
| | 55 | % Get number of dimensions of each input
|
< 0.001 | 5 | 56 | twod = (cellfun('ndims', varargin) == 2);
|
| | 57 |
|
| | 58 | % Return empty string when all inputs are empty
|
< 0.001 | 5 | 59 | if all(rows == 0)
|
| | 60 | return;
|
| | 61 | end
|
< 0.001 | 5 | 62 | if ~all(twod)
|
| | 63 | error(message('MATLAB:strfun:InputDimension'));
|
| | 64 | end
|
| | 65 |
|
| | 66 | % Remove empty inputs
|
< 0.001 | 5 | 67 | k = (rows == 0);
|
< 0.001 | 5 | 68 | varargin(k) = [];
|
< 0.001 | 5 | 69 | rows(k) = [];
|
< 0.001 | 5 | 70 | maxrows = max(rows);
|
| | 71 | % Scalar expansion
|
| | 72 |
|
< 0.001 | 5 | 73 | for i = 1:length(varargin)
|
< 0.001 | 10 | 74 | if rows(i) == 1 && rows(i) < maxrows
|
| | 75 | varargin{i} = varargin{i}(ones(1,maxrows), :);
|
| | 76 | rows(i) = maxrows;
|
| | 77 | end
|
< 0.001 | 10 | 78 | end
|
| | 79 |
|
< 0.001 | 5 | 80 | if any(rows ~= rows(1))
|
| | 81 | error(message('MATLAB:strcat:NumberOfInputRows'));
|
| | 82 | end
|
| | 83 |
|
< 0.001 | 5 | 84 | n = rows(1);
|
< 0.001 | 5 | 85 | space = sum(cellfun('prodofsize', varargin));
|
0.003 | 5 | 86 | s0 = blanks(space);
|
< 0.001 | 5 | 87 | scell = cell(1, n);
|
< 0.001 | 5 | 88 | notempty = true(1, n);
|
< 0.001 | 5 | 89 | s = '';
|
< 0.001 | 5 | 90 | for i = 1:n
|
< 0.001 | 5 | 91 | s = s0;
|
< 0.001 | 5 | 92 | input = varargin{1}(i, :);
|
< 0.001 | 5 | 93 | if ~isempty(input) && (input(end) == 0 || isspace(input(end)))
|
| | 94 | input = char(deblank(input));
|
| | 95 | end
|
< 0.001 | 5 | 96 | pos = length(input);
|
< 0.001 | 5 | 97 | s(1:pos) = input;
|
< 0.001 | 5 | 98 | pos = pos + 1;
|
< 0.001 | 5 | 99 | for j = 2:length(varargin)
|
< 0.001 | 5 | 100 | input = varargin{j}(i, :);
|
< 0.001 | 5 | 101 | if ~isempty(input) && (input(end) == 0 || isspace(input(end)))
|
| | 102 | input = char(deblank(input));
|
| | 103 | end
|
< 0.001 | 5 | 104 | len = length(input);
|
< 0.001 | 5 | 105 | s(pos:pos+len-1) = input;
|
< 0.001 | 5 | 106 | pos = pos + len;
|
< 0.001 | 5 | 107 | end
|
< 0.001 | 5 | 108 | s = s(1:pos-1);
|
< 0.001 | 5 | 109 | notempty(1, i) = ~isempty(s);
|
< 0.001 | 5 | 110 | scell{1, i} = s;
|
< 0.001 | 5 | 111 | end
|
< 0.001 | 5 | 112 | if n > 1
|
| | 113 | t = char(scell{notempty});
|
< 0.001 | 5 | 114 | else
|
< 0.001 | 5 | 115 | t = s;
|
< 0.001 | 5 | 116 | end
|
Other subfunctions in this file are not included in this listing.