-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_string.m
More file actions
70 lines (45 loc) · 1.83 KB
/
split_string.m
File metadata and controls
70 lines (45 loc) · 1.83 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title: split_string
% Date: 16.11.2017
% Version 0.01
% Author: Robin Eccleston
% Description:
%
% Existing in newer versions of matlab but not the one I have. This
% removes empty spaces so doesnt preserve structure. It also removese the
% delimier. Not fully tested.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function output = split_string(this_string, delimiter)
%this_string='a,,,a,,bcabcabc,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,a,,b,,c,,d,,e,,f,,g,,h,,i,,j,,k,,l,a,,,b,,,c,,,d,,e,,,f,,,g,'
%delimiter=','
%while the string starts with the delimiter, remove the delimiter
del_len=size(delimiter,2);
while (this_string(1:del_len)==delimiter)
this_string(1:del_len)=[];
end
%find all the points that a string is split
%split_points=strfind(this_string, delimiter);
split_points = regexp(this_string, delimiter);
split_points=[0, split_points];
%get the number of points where we split it
num_split_points=size(split_points,2)-1;
output='';
counter=1;
%loop through each point to split the string
if split_points(1)==1;
end
for i=1:num_split_points
start_pos=split_points(i)+1;
end_pos=split_points(i+1)-1;
if end_pos>=start_pos
this_section=this_string(split_points(i)+1:split_points(i+1)-1);
output{counter}=this_section;
counter=counter+1;
end
end
if ~(split_points(end)==size(this_string,2))
this_section=this_string(split_points(i+1)+1:size(this_string,2));
output{counter}=this_section;
end
end