why does str2num create a figure?

조회 수: 2 (최근 30일)
Pepijn
Pepijn 2012년 2월 16일
편집: Jan 2013년 10월 14일
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
Jan
Jan 2012년 2월 16일
Thanks for pointing to this old problem.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2012년 2월 16일
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
Andreas Goser
Andreas Goser 2012년 2월 16일
The published MathWorks solution can be found here: http://www.mathworks.de/support/solutions/en/data/1-2ED43R/index.html

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Pepijn
Pepijn 2012년 2월 16일
Thanks Jan!
Str2double doens't really do the job either since it isn't converting multiple entries, str2double('1 2') gives NaN. So I'll stick with the workaround below.
Workaround: using an "if sum(isletter(string))==0, do something", first.
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 2월 16일
str2double(regexp(string, 'split'))
Jan
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by