What's difference between str2num and str2double for converting an array to numeric values?

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 Comments

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'}
str = 1×3 cell array
{'1'} {'2'} {'1 3'}
str2double(str)
ans = 1×3
1 2 NaN
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.
Thank you for your answer! I am trying to convert stings in an array of 84x1 to numeric values. The strings in the array are of the form:
{'50.04'
'60.10'
etc}
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.
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.
@Walter Roberson Thank you for your concise answer. I would have hit accept answer but the option is not available for replies.

Sign in to comment.

 Accepted Answer

str2num(chr) converts a character array or string scalar to a numeric matrix. The input can include spaces, commas, and semicolons to indicate separate elements. If str2num cannot parse the input as numeric values, then it returns an empty matrix.
The str2num function does not convert cell arrays or nonscalar string arrays, and is sensitive to spacing around + and - operators.
str2num() contains a call to eval(), which means that if your string has the same form as an array (e.g. with semicolons) then it is simply executed and the resulting array is returned. The problems with str2num() are that it doesn’t support cell arrays, and that because it uses an eval() function, wierd things can happen if your string includes a function call. str2double() is supposedly faster as well. So the general rule seems to be, use str2double() unless you are wanting to convert a string array of numbers to a numerical array.
There is one catch though- if you are converting a single number from a string to a number, then it would SEEM like str2num() and str2double() are interchangable, drop in replacements. However, if the string is not a number, they behave differently

1 Comment

"...unless you are wanting to convert a string array of numbers to a numerical array"
in which case SSCANF is much faster than STR2NUM.
"So the general rule seems to be"
use SSCANF if you can!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!