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 件のコメント

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

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2016 年 3 月 16 日

0 投票

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

質問済み:

2016 年 3 月 16 日

回答済み:

2016 年 3 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by