Most people have used str2num() at one point or another, but if you pay attention to the coding tips generated by the Matlab editor (or mlint) then you would have noticed that they suggest using str2double() instead if you are not working on arrays. Why?

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

str2num('x') -> []
str2double('x') -> NaN

Returning an NaN makes sense, because ‘x’ is not a number. However, if your original code tests ~isempty(str2num(string))) to see if the string is a number (this expression returns 1 if the string was a number, and 0 otherwise), then it will fail. Instead, you would need something like isfinite(str2double(string))).

Incidentally, str2num() returns a second variable that is 1 if the string was a number and 0 otherwise, so it would have been a better idea to do something like this:

 [number,valid] = str2num(string)

and then check the variable valid to see if string was a number or not.