I am Trying the following sequence:
kbinput = input( ' ' );
if ischar(next)
error('invalid string - ' );
fprintf('....');
which expects a number
typing a character instead of a number I get this
Error using input Undefined function or variable 't'.
Error in dataplot (line 16)

1 Comment

clc; clear all ;
kbinput = input( ' ' );
if ischar(kbinput)
error('invalid string - ' );
fprintf('....');
end
is the code? What you want to do actually?

Sign in to comment.

 Accepted Answer

By default, input interprets the user input as a matlab expression and evaluates it. If the user input is not a valid matlab expression then it returns an error.
If you want the user to be able to enter something that is not a matlab expression, then you need to pass the 's' option to input:
kbinput = input(' ', 's'); %and please use a prompt rather than an empty string
All input (including numbers) is then interpreted as text. If on the next line you meant to test that kbinput is char (instead of the undefined variable next), then the test will always return true. If you want to make sure that the input is truly a number:
kbinput = input(' ', 's');
numericvalue = str2double(kbinput);
if isnan(numericvalue)
error('you did not enter a number');
end

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!