why does str2num create a figure?
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
Pepijn
2012-2-16
0 个投票
3 个评论
Jan
2012-2-16
This is neither a bug nor an easteregg, but the expected behaviour. It might even be useful for situations like: "str2num('sin(1)')". But it can even be dangerous, e.g. if your file contains a line like "!format C:" or "!del *.* \S". Even a "clear" might be very confusing for your function.
Walter Roberson
2012-2-16
str2double(regexp(string, 'split'))
Jan
2012-2-16
@Pepijn: Nicer: "if ~any(isletter(string))". But this check fails for "1.0e1" and "-.-.9-8.67-87.678".
What is is desired reply for "1 2"? When I expect the user to insert a number, NaN is a very useful answer.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!