from
String to Cells
by Cole Stephens
Split a single string into a cell array.
|
| str2cells(someString)
|
function newCell = str2cells(someString)
%Take a single string and separate out individual "elements" into a new
%cell array. Elements are defined as non-blank characters separated by
%spaces.
%
%Similar to str2cell, except str2cell requires an array of strings.
%str2cells requires only 1 string.
%
%Example: Consider the following string in the workspace:
%aString = ' a b c d efgh ij klmnopqrs t u v w xyz '
%
% >> newCell=str2cells(aString)'
%
% newCell =
%
% 'a'
% 'b'
% 'c'
% 'd'
% 'efgh'
% 'ij'
% 'klmnopqrs'
% 't'
% 'u'
% 'v'
% 'w'
% 'xyz'
% Trim off any leading & trailing blanks
someString=strtrim(someString);
% Locate all the white-spaces
spaces=isspace(someString);
% Build the cell array
idx=0;
while sum(spaces)~=0
idx=idx+1;
newCell{idx}=strtrim(someString(1:find(spaces==1,1,'first')));
someString=strtrim(someString(find(spaces==1,1,'first')+1:end));
spaces=isspace(someString);
end
newCell{idx+1}=someString;
|
|
Contact us at files@mathworks.com