Inputdlg accept only numbers

Beginner in MATLAB Simply program, to construct a 3x3 matrix and use inputdlg but accept only numbers Tried all answered previously can't get them to work,cant even get isnumeric to work I just want a statement saying: if (the value entered is anything other than a number) disp (please enter only numbers) else continue end but just cant seem to get this working, if i enter 'a' for example, it displays a in the matrix

回答 (1 件)

Image Analyst
Image Analyst 2013 年 11 月 19 日

0 投票

Try this snippet:
% Ask user for a number.
defaultValue = 45.67;
titleBar = 'Enter a value';
userPrompt = 'Enter the number';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
numericValue = str2double(cell2mat(caUserInput)); % May or may not be.
% Check for a valid number.
if isnan(numericValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
numericValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %f and continue.', numericValue);
uiwait(warndlg(message));
end

2 件のコメント

Rob
Rob 2013 年 11 月 19 日
can i email you my code to have a look at it? please!
Image Analyst
Image Analyst 2013 年 11 月 19 日
No, but you can attach it here and people may look at it.

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

カテゴリ

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

タグ

質問済み:

Rob
2013 年 11 月 18 日

コメント済み:

2013 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by