MATLAB Input Function Help | Elbarto

Click For Summary
SUMMARY

The discussion focuses on handling user input in MATLAB to ensure only numeric values are accepted. The user, Elbarto, initially attempted to use the input('prompt','s') command but sought a more effective solution that allows for expressions like 5/100. The recommended approach involves using the isnumeric() function to validate input and the str2num() function to convert strings to numbers. Additionally, the discussion highlights that 5/100 is treated as an expression in MATLAB, not a direct numeric input.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with the isnumeric() function
  • Knowledge of the str2num() function for string conversion
  • Basic concepts of user input handling in programming
NEXT STEPS
  • Learn how to implement input validation in MATLAB using isnumeric()
  • Explore the usage of str2num() for converting strings to numeric values
  • Research error handling techniques in MATLAB to manage invalid inputs
  • Investigate alternatives to eval() for evaluating mathematical expressions in MATLAB
USEFUL FOR

MATLAB developers, data analysts, and anyone looking to improve user input validation in MATLAB applications.

elbarto
Messages
32
Reaction score
0
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 don't want to use this command because a user can't 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:
Physics news on Phys.org
elbarto said:
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 don't want to use this command because a user can't 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.
 
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.

https://www.mathworks.com/help/matlab/ref/str2num.html
https://www.mathworks.com/help/matlab/ref/input.html
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K