forked from BUNPC/Homer3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
str2cell.m
46 lines (40 loc) · 1.1 KB
/
str2cell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function [C,k] = str2cell(str, delimiters, options)
% Option tells weather to keep leading whitespaces.
% (Trailing whitespaces are always removed)
if ~exist('options','var')
options = '';
end
if ~strcmpi(options, 'keepblanks')
str = strtrim(str);
end
str = deblank(str);
if ~exist('delimiters','var') || isempty(delimiters)
delimiters{1} = sprintf('\n');
elseif ~iscell(delimiters)
foo{1} = delimiters;
delimiters = foo;
end
% Get indices of all the delimiters
k=[];
for kk=1:length(delimiters)
k = [k, find(str==delimiters{kk})];
end
j = find(~ismember(1:length(str),k));
% The following line seems to hurt performance a little bit. It was
% meant to preallocate to speed things up but it does seem to do that.
% C = repmat({blanks(max(diff([k,length(str)])))}, length(k)+1, 1);
C = {};
ii=1; kk=1;
while ii<=length(j)
C{kk} = str(j(ii));
ii=ii+1;
jj=2;
while (ii<=length(j)) && ((j(ii)-j(ii-1))==1)
C{kk}(jj) = str(j(ii));
jj=jj+1;
ii=ii+1;
end
C{kk}(jj:end)='';
kk=kk+1;
end
C(kk:end) = [];