Why 'str2double' is preferred over 'str2num?
Ältere Kommentare anzeigen
Many times while writting matlab code, matlab forces to use 'str2double' function instead of 'str2num'.
I searched a lot, but could not get a convincing answer. Anyobody here has any idea?
Akzeptierte Antwort
Weitere Antworten (2)
Daniel Shub
am 30 Okt. 2012
In this case I think it is best to actually look at the code of the two functions
type str2num
type str2double
One problem with str2num is that it uses EVAL. A malicious user could therefore cause major problems if str2num is used. That said, the use of eval within str2num is one of those cases where it is well encapsulated and unlikely to cause problems (but as I will point out below, can still lead to unexpected outcomes). It will in general be slow, but often that is not a big deal.
The real advantage is that str2double is a lot more powerful. It can handle all sorts of representations of numbers. For example, str2double can handle 1,000, while str2num cannot. In fact, str2num does not give you an error, but rather [1, 0], which might be unexpected.
1 Kommentar
Sachin Ganjare
am 31 Okt. 2012
Jan
am 30 Okt. 2012
STR2MUM evaluates the string using EVAL. Therefore there might be unexpected side effects:
str2num('system(''format C:''); fprintf(''9\n'')');
1 Kommentar
Sachin Ganjare
am 31 Okt. 2012
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!