MATLAB Input Function Help | Elbarto

In summary, the conversation is about a user seeking help with their MATLAB code to terminate the program if a non-numeric value is inputted. They discuss using the isnan() function and suggest using the isnumeric() function instead. They also mention using the str2num() function to convert a string to a number. The user is concerned about not being able to enter certain expressions, such as 5/100, but is advised that this can be evaluated as a number. The conversation concludes with a suggestion to search for help on converting strings to numbers on Google.
  • #1
elbarto
33
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
  • #2
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.
 
  • #3
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:

1. What is the purpose of the MATLAB input function?

The MATLAB input function is used to prompt the user to enter data into the command window during a program execution. This allows for user interaction and input in order to run the program in a more dynamic manner.

2. How do I use the MATLAB input function?

To use the MATLAB input function, you can simply type the function name followed by parentheses in the command window. Within the parentheses, you can specify a prompt for the user to see before entering their input. The function will then wait for the user to enter their data and press enter.

3. What is the syntax for the MATLAB input function?

The syntax for the MATLAB input function is as follows:
input(prompt)
Where "prompt" is an optional string that prompts the user for input. If no prompt is specified, the function will simply wait for user input without displaying anything.

4. Can the MATLAB input function accept multiple inputs?

Yes, the MATLAB input function can accept multiple inputs separated by spaces. For example, if the user enters "1 2 3", the function will return a 1x3 array with each number as an element.

5. Is the MATLAB input function case-sensitive?

No, the MATLAB input function is not case-sensitive. This means that "Hello" and "hello" will be treated as the same input. However, it is important to note that the input function will return the user's input exactly as it was entered, so any capitalization will be preserved.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
230
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
859
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top