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 commentaire

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

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 16 Mar 2016

0 votes

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

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by