Convert String to Numerical Matrix
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi there,
Just wondering how to convert some matrix string (such as A = '[1,2,3,4,5]') into a numerical matrix (like [1,2,3,4,5]) so that it displays when entered into matlab like,
ans =
1 2 3 4 5
instead of
Columns 1 through 5
1.0000 2.0000 3.0000 4.0000 5.0000
Cheers,
Samuel
채택된 답변
madhan ravi
2020년 10월 10일
편집: madhan ravi
2020년 10월 10일
Normally str2num() is not suggested:
Wanted = sscanf(A(2:end-1), '%d').' % edited after sir Walter’s comment to exclude []
댓글 수: 5
Walter Roberson
2020년 10월 10일
A = '[1 0.2 0.4 0.5 0.6]'; sscanf(A(2:end-1), '%f')
If the problem is that you might have commas or might not then:
A = '[1 0.2, 0.4, 0.5 0.6]'; sscanf(A(2:end-1), '%f%*[, ]')
추가 답변 (1개)
Ameer Hamza
2020년 10월 10일
편집: Ameer Hamza
2020년 10월 10일
A = '[1,2,3,4,5]';
A = str2num(A);
Result
>> A
A =
1 2 3 4 5
A = '[1,2,3,4,5]';
A = eval(A);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!