time | Calls | line |
---|
| | 1 | function c = cellstr(s)
|
| | 2 | %CELLSTR Create cell array of character vectors
|
| | 3 | % C = CELLSTR(S) converts S to a cell array of character vectors.
|
| | 4 | % If S is a string array, then CELLSTR converts each element of S.
|
| | 5 | % If S is a character array, then CELLSTR places each row into a
|
| | 6 | % separate cell of C. Any trailing spaces in the character vectors are
|
| | 7 | % removed.
|
| | 8 | %
|
| | 9 | % Use STRING to convert C to a string array, or CHAR to convert C
|
| | 10 | % to a character array.
|
| | 11 | %
|
| | 12 | % Another way to create a cell array of character vectors is by using
|
| | 13 | % curly braces:
|
| | 14 | % C = {'hello' 'yes' 'no' 'goodbye'};
|
| | 15 | %
|
| | 16 | % See also STRING, CHAR, ISCELLSTR.
|
| | 17 |
|
| | 18 | % Copyright 1984-2016 The MathWorks, Inc.
|
| | 19 |
|
< 0.001 | 132 | 20 | if ischar(s)
|
< 0.001 | 124 | 21 | if isempty(s)
|
| | 22 | c = {''};
|
< 0.001 | 124 | 23 | elseif ~ismatrix(s)
|
| | 24 | error(message('MATLAB:cellstr:InputShape'))
|
< 0.001 | 124 | 25 | else
|
< 0.001 | 124 | 26 | numrows = size(s,1);
|
< 0.001 | 124 | 27 | c = cell(numrows,1);
|
< 0.001 | 124 | 28 | for i = 1:numrows
|
< 0.001 | 124 | 29 | c{i} = s(i,:);
|
< 0.001 | 124 | 30 | end
|
< 0.001 | 124 | 31 | c = deblank(c);
|
< 0.001 | 124 | 32 | end
|
< 0.001 | 8 | 33 | elseif iscellstr(s)
|
< 0.001 | 8 | 34 | c = s;
|
| | 35 | elseif iscell(s)
|
| | 36 | c = cell(size(s));
|
| | 37 | for i=1:numel(s)
|
| | 38 | if ischar(s{i}) || (isstring(s{i}) && isscalar(s{i}) && ~ismissing(s{i}))
|
| | 39 | c{i} = char(s{i});
|
| | 40 | else
|
| | 41 | if (isstring(s{i}) && isscalar(s{i})) && ismissing(s{i})
|
| | 42 | error(message('MATLAB:string:CannotConvertMissingElementToChar', i));
|
| | 43 | else
|
| | 44 | error(message('MATLAB:cellstr:MustContainText', i));
|
| | 45 | end
|
| | 46 | end
|
| | 47 | end
|
| | 48 | else
|
| | 49 | error(message('MATLAB:invalidConversion', 'cellstr', class(s)));
|
< 0.001 | 132 | 50 | end
|
Other subfunctions in this file are not included in this listing.