Matlab input function

  • Thread starter elbarto
  • Start date
Hi all,
Can someone please help me with my matlab code. I am trying to get the program to quite if the user tries to input a non-numeric value, and will display an error message. I have got it to work useing the matlab input('promt','s') command, but i dont want to use this command because a user cant enter 5/100 etc.
Here is my code.

Matlab:
j = input('number?:')
if isnan(j)==1
    disp('Input must be a number, Function will terminate')
    return
end
I would just like to know if there is a simply way to do this, if not I will use the command I used before and use the eval function I think...

Thank You

Regards Elbarto
 
Last edited by a moderator:
32,344
4,130
Hi all,
Can someone please help me with my matlab code. I am trying to get the program to quite if the user tries to input a non-numeric value, and will display an error message. I have got it to work useing the matlab input('promt','s') command, but i dont want to use this command because a user cant enter 5/100 etc.
Here is my code.

Matlab:
j = input('number?:')
if isnan(j)==1
    disp('Input must be a number, Function will terminate')
    return
end
I would just like to know if there is a simply way to do this, if not I will use the command I used before and use the eval function I think...

Thank You

Regards Elbarto
The matlab isnan() function is not what you want to use. Instead, the isnumeric() function will evaluate to logical 1 (true) if the argument is a number, and logical 0 (false) if not, such as for a string or logical expression.
 
10,334
3,866
Another approach is to accept a string and check if it’s a q to quit and if not evaluate it or convert it to a number.



Please note that 5/100 is not a number in the Matlab sense rather it’s an expression to be evaluated to a number whereas 0.05 it’s equivalent is a number.
Matlab:
ans=input(‘enter number or q to quit ==>’,’s’)

if ans==‘q’
    quit
else
  num=str2num(ans)
end
You can find a lot of help if you use google by searching on “Matlab convert string to number” as an example. Matlab has a large base of helpful pages on all things Matlab and google has indexed everyone of them..
 
Last edited:

Want to reply to this thread?

"Matlab input function" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top