Borrar filtros
Borrar filtros

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)
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
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]]'
?
Chris
Chris el 23 de Jun. de 2017
Editada: Chris el 23 de Jun. de 2017
Yes numbers as you put it.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
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
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),'],[',';'));

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!

Translated by