Instead of outputting error how to display warning?

1 回表示 (過去 30 日間)
Dc215905
Dc215905 2021 年 7 月 1 日
回答済み: Animesh 2023 年 2 月 9 日
I have a GUI where users are typing values into a table. In order for the calculations to work they have to type in numbers and not letters. This way i can convert the numbers using str2num.
Is there a way to check if str2num will work, and if it doen't then I can display a msgbox telling the users to only type in numbers?
  3 件のコメント
Yazan
Yazan 2021 年 7 月 1 日
You may check if the user enters a number as follws:
x = input('Insert a number\n', 's');
if isnan(str2double(x))
error('Enter a number without letters');
end
Dc215905
Dc215905 2021 年 7 月 1 日
Thank you!

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

回答 (1 件)

Animesh
Animesh 2023 年 2 月 9 日
Hello,
According to my understanding you want to display a custom message when the user inputs wrong data format.
We can display a custom error message with the help of error function.
Sample Code: -
x = input("Input number\n",'s') %Input from the user
if isempty(str2num(x))
error("The input is not of correct format.\n")
end
For further reference on error use: - https://www.mathworks.com/help/matlab/ref/error.html
For further reference on str2num use: - https://www.mathworks.com/help/matlab/ref/str2num.html

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by