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

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

1 comentario

Jan
Jan el 14 de Oct. de 2013
Editada: Jan el 14 de Oct. de 2013
"String" means a vector of type char in Matlab. Your data look like a matrix instead. Or do you mean:
Str = '1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18'
?? Should the new "string" be a cell string or a string, which contains line break characters?

Iniciar sesión para comentar.

 Respuesta aceptada

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{:}

Más respuestas (4)

Rogier
Rogier el 14 de Oct. de 2013
Editada: Rogier el 14 de Oct. de 2013
sorry for the slow reaction, but thanks alot! If i can bother you again... how would you implement adding some text inbetween the breaks? like
stringnumber1
1 2 3 4 5
6
Text
7 8 9 10 11
12
Another line
13 14 15 16 17
18
Jos (10584)
Jos (10584) el 14 de Oct. de 2013
Editada: Jos (10584) el 14 de Oct. de 2013
Something along these lines, perhaps (untested!):
% data:
str = '1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18'
TextToInsert = {'Text','Another line'}
% engine:
ss = strread(str,'%s','delimiter',';') ;
C = {} ;
t2i_counter = 1 ;
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
if t2i_counter < numel(TextToInsert)
C(end+1) = TextToInsert(t2i_counter) ;
t2i_counter = t2i_counter + 1 ;
else
C{end+1} = 'Default insert text' ;
end
end
% result:
C{:}
Jan
Jan el 14 de Oct. de 2013
Str = '1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18'
Str = strrep(Str, ';', '');
CStr = regexp(Str, ' ', 'split');
NL = char(10);
Header = {'stringnumber1', 'Text', 'Another line'};
CStr(1:6:end) = strcat(Header, NL, CStr(1:3:end));
CStr(5:6:end) = strcat(CStr(5:6:end), NL);
CStr(6:6:end) = strcat(CStr(6:6:end), NL);
fprintf('%s ', CStr{:})
(Not tested!)
Rogier
Rogier el 15 de Oct. de 2013
Editada: Rogier el 15 de Oct. de 2013
Allright then! So i now got the following and this works for what i want! Thanks alot
TextToInsert = {'Txt','Another line', 'txt314',};
% engine:
ss = strread(L,'%s','delimiter',';');
C = {};
C{1} = TextToInsert(1);
t2i_counter = 1;
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
if t2i_counter < numel(TextToInsert);
C{end+1} = TextToInsert(t2i_counter+1) ;
t2i_counter = t2i_counter + 1;
end
end
% result:
C = transpose(C);
Output of C gives me : {1x1 cell } {1x1 cell } {1x1 cell } {1x1 cell } {1x1 cell } {1x1 cell } {1x1 cell } [1x5 double] [ 6] {1x1 cell } [1x5 double] [ 12] {1x1 cell } [1x5 double] [ 18]
How would I write this into a text file? make a loop in which i convert the doubles with dlmwrite and do the cells with fprintf with the use of append?
EDIT: nvm guys i fixed it! thanks anyway

Categorías

Etiquetas

Preguntada:

el 10 de Oct. de 2013

Editada:

el 15 de Oct. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by