What is the simplest way to convert this string '[[x1, x2, x3], [y1, y2, y3], [& so on]]' into a matlab matrix where each item is a row, [x1,x2,x3 ; y1, y2, y3; & so on]?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chris
el 23 de Jun. de 2017
Comentada: Chris
el 26 de Jun. de 2017
What is the simplest way to convert this string '[[x1, x2, x3], [y1, y2, y3], [& so on]]' into a matlab matrix where each item is a row, [x1,x2,x3 ; y1, y2, y3; & so on]? Thank you!
2 comentarios
the cyclist
el 23 de Jun. de 2017
Editada: the cyclist
el 23 de Jun. de 2017
In the string, are x1, etc, numbers? So is an example string
'[[1,2,3],[4,5,6]]'
?
Respuesta aceptada
Walter Roberson
el 23 de Jun. de 2017
S = '[[x1, x2, x3], [y1, y2, y3], [&, so, on]]';
S = S(3:end-2); %get rid of leading and trailing [[ ]]
by_line = regexp(S, '],\s*\[', 'split'); %split at comma between subsections
by_item = regexp(by_line, ',\s*', 'split'); %split each subsection at comma
enmass = vertcat(by_item{:}); %reassemble cell array
output = str2double(enmass);
Más respuestas (1)
the cyclist
el 23 de Jun. de 2017
Similar technique to Walter's:
S = '[[1,2,3],[4,5,16]]';
output = str2num(regexprep(S(3:end-2),'],[',';'));
2 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!