forked from shanqing-cai/commonmcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring2intervals.m
More file actions
70 lines (62 loc) · 1.86 KB
/
Copy pathstring2intervals.m
File metadata and controls
70 lines (62 loc) · 1.86 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function out = string2intervals(str, bCheckOrder)
%% Constants
sep = ',';
%%
out = {};
str = strrep(str, ' ', '');
if ~isequal(str(end), sep)
str = [str, sep];
end
idx = 1;
bBracket = 0;
tmpStr = [];
while idx <= length(str)
if bBracket == 0
if isequal(str(idx), '[')
bBracket = 1;
elseif isequal(str(idx), ']')
error('Unexpected right bracket');
elseif isequal(str(idx), sep)
if isempty(strfind(tmpStr, '-'))
out{end + 1} = repmat(str2double(tmpStr), 1, 2);
elseif length(strfind(tmpStr, '-') == 1)
t_vals = splitstring(tmpStr, '-');
t_array = [str2double(t_vals{1}), str2double(t_vals{2})];
if bCheckOrder
t_array = sort(t_array);
end
out{end + 1} = t_array;
end
tmpStr = [];
else
tmpStr = [tmpStr, str(idx)];
end
else
if isequal(str(idx), ']')
bBracket = 0;
if isempty(strfind(tmpStr, '-'))
out{end + 1} = repmat(str2double(tmpStr), 1, 2);
elseif length(strfind(tmpStr, '-') == 1)
t_vals = splitstring(tmpstr, '-');
t_array = [str2double(t_vals{1}), str2double(t_vals{2})];
if bCheckOrder
t_array = sort(t_array);
end
out{end + 1} = t_array;
end
tmpStr = [];
elseif isequal(str(idx), '[')
error('Unexpected left bracket');
else
tmpStr = [tmpStr, str(idx)];
end
end
idx = idx + 1;
end
%% Prune results
bPreserve = zeros(1, length(out));
for i1 = 1 : length(out)
bPreserve(i1) = ~isnan(out{i1}(1));
end
out = out(find(bPreserve));
return