why does str2num create a figure?
2 views (last 30 days)
Show older comments
I am using the property that str2num(string) returns an empty when the string is not solely made of numbers. In my case, I want to ignore the lines of my file that are not numers only so if ~isempty(str2num(fgetl(fid))) do something. However, a window popped up. It turns out "lines 42" - an entry in my file - caused this. Upon looking into it, I found
str2num('lines 1') or variations pop up a figure window; str2num('line') actually draws a line from (0,0) to (1,1). Also, str2num('lines') returns a matrix of values.
Why? And moreover, is there a workaround or a function I can use instead?
Accepted Answer
Jan
on 16 Feb 2012
STR2NUM uses eval to get the numerical contents of a string. If the string contains a Matlab command like line, it is evaluated with the obeserved side-effects. This is explained in the documentation, see help str2num. There you find the recommendation to use str2double to avoid this. In addition you can use sscanf also, but this accepts lines like "3.14 dummy" also by ignoring everything behind the valid number.
1 Comment
Andreas Goser
on 16 Feb 2012
The published MathWorks solution can be found here: http://www.mathworks.de/support/solutions/en/data/1-2ED43R/index.html
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!