Filter löschen
Filter löschen

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 Ansichten (letzte 30 Tage)
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 Kommentare
the cyclist
the cyclist am 23 Jun. 2017
Bearbeitet: the cyclist am 23 Jun. 2017
In the string, are x1, etc, numbers? So is an example string
'[[1,2,3],[4,5,6]]'
?
Chris
Chris am 23 Jun. 2017
Bearbeitet: Chris am 23 Jun. 2017
Yes numbers as you put it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Jun. 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);

Weitere Antworten (1)

the cyclist
the cyclist am 23 Jun. 2017
Similar technique to Walter's:
S = '[[1,2,3],[4,5,16]]';
output = str2num(regexprep(S(3:end-2),'],[',';'));

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by