MATLAB: How do i cut a string after an amount of values or a delimiter

string

Hey was hoping someone could help me out here.
I have a string lets say:
1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18
Now i want to make a new string that always cuts of after the first 5 values until it reaches the ;. So in this case I would get an output like
1 2 3 4 5
6
7 8 9 10 11
12
13 14 15 16 17
18
Any help would be appreciated…
Roger

Best Answer

Here is a straighforward approach:
% data:
str = '1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18'
% engine:
ss = strread(str,'%s','delimiter',';') ;
C = {} ;
for k1=1:numel(ss),
X = str2num(ss{k1}) ;
nX = numel(X) ;
for k2=1:5:nX
C{end+1} = X(k2:min(k2+4,nX)) ;
end
end
% result:
C{:}