What's difference between str2num and str2double for converting an array to numeric values?
Mostra commenti meno recenti
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 Commenti
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
il 3 Ago 2021
Modificato: ardeshir moinian
il 3 Ago 2021
Rik
il 3 Ago 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
il 3 Ago 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
il 4 Ago 2021
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Data Type Conversion in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!