필터 지우기
필터 지우기

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 (최근 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!
  댓글 수: 2
the cyclist
the cyclist 2017년 6월 23일
편집: the cyclist 2017년 6월 23일
In the string, are x1, etc, numbers? So is an example string
'[[1,2,3],[4,5,6]]'
?
Chris
Chris 2017년 6월 23일
편집: Chris 2017년 6월 23일
Yes numbers as you put it.

댓글을 달려면 로그인하십시오.

채택된 답변

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