What's difference between str2num and str2double for converting an array to numeric values?
Ältere Kommentare anzeigen
Hi, I have a 'char' array and want to convert it to a numeric array. when I use the function
str2num
I get the following error:
"Error using str2num (line 35)
Input must be a character vector or string scalar."
but I can change the format of the array using
str2double
can someone tell me why does the latter work?
5 Kommentare
str2num calls eval to convert a character vector to a number. That should be reason enough to avoid it.
str2double supports a wide range of container data types, but it can only convert to scalar values:
str={'1','2','1 3'}
str2double(str)
So why does str2double work for you? It must be because the specific format you use. But since you didn't post what you actually used, it is impossible to reproduce what went wrong with str2num.
ardeshir moinian
am 3 Aug. 2021
Bearbeitet: ardeshir moinian
am 3 Aug. 2021
Rik
am 3 Aug. 2021
What you posted are char vectors, not strings. Details matter. If you want help with your actual data, you will have to post at least a working snippet of your actual data. I see no indication in what you posted why one would work, but not the other.
Walter Roberson
am 3 Aug. 2021
str2num() will not work with cell array of character vectors.
str2double() will work with cell array of character vectors.
str2num() will work with char array with multiple rows.
str2double() will not work with char array with multiple rows.
ardeshir moinian
am 4 Aug. 2021
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!