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]?

4 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2017-6-23
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);

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by