Convert String to Numerical Matrix
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Samuel Lee
am 10 Okt. 2020
Kommentiert: Samuel Lee
am 10 Okt. 2020
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
Akzeptierte Antwort
madhan ravi
am 10 Okt. 2020
Bearbeitet: madhan ravi
am 10 Okt. 2020
Normally str2num() is not suggested:
Wanted = sscanf(A(2:end-1), '%d').' % edited after sir Walter’s comment to exclude []
5 Kommentare
Walter Roberson
am 10 Okt. 2020
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%*[, ]')
Weitere Antworten (1)
Ameer Hamza
am 10 Okt. 2020
Bearbeitet: Ameer Hamza
am 10 Okt. 2020
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 Kommentare
Siehe auch
Kategorien
Mehr zu Logical 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!