Easy question comparing strings

12 Ansichten (letzte 30 Tage)
JP
JP am 26 Jun. 2013
Hello I have the following code which doesn't work. The problem is Im trying to compare the value "NaN" which I guess isnt a string?...to the value of X. Let me know how to fix it thanks!
str = input('Please enter a number \n\n','s');
X = str2double(str);
test=strcmp(X,'NaN');
if test == 1
disp('That was not a valid number');
else
disp('Here is your number')
disp(X)
end

Akzeptierte Antwort

Tom
Tom am 26 Jun. 2013
You can just ISNAN instead:
str = input('Please enter a number \n\n','s');
X = str2double(str);
if isnan(X)
disp('That was not a valid number');
else
disp('Here is your number')
disp(X)
end

Weitere Antworten (0)

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by