Adding a Warning Window/Message In Matlab App Designer

I am new to Matlab, and I am trying to figure out how I can show a warning message once either the user's input is zero or the edit field is left blank.
Do you guys know how to do it? I honestly do not know how to put warning message in Matlab App Designer. Thanks!

回答 (1 件)

Simon Chan
Simon Chan 2022 年 2 月 21 日

1 投票

You may use function uialert to deliver a message to the user via a callback
Please refer to the following example and modify using App Designer.
uif = uifigure('Position',[100 100 500 250]);
edit = uieditfield(uif,'Position',[50 50 50 30],'ValueChangedFcn',{@checkValue,uif});
function checkValue(src,event,uif)
value = get(src,'Value');
if isempty(value)
uialert(uif,'Please enter a non-zero number, it is empty now','Warning','Icon','warning');
elseif str2double(value)==0
uialert(uif,'Please enter a non-zero number, it is zero now','Warning','Icon','warning');
end
end

6 件のコメント

Mayvel Reese
Mayvel Reese 2022 年 2 月 21 日
Unfortunately, it does not work with me. I tried having one edit field and modifying it afterwards.
unit=app.EditField.Value;
uif = uifigure('Position',[100 100 500 250]);
edit = app.EditField(uif,'Position',[50 50 50 30],'ValueChangedFcn',{@checkValue,uif});
function checkValue(src,event,uif)
unit = get(src,'Value');
if isempty(unit)
uialert(uif,'Please enter a non-zero number, it is empty now','Warning','Icon','warning');
elseif str2double(unit)==0
uialert(uif,'Please enter a non-zero number, it is zero now','Warning','Icon','warning');
end
end
Simon Chan
Simon Chan 2022 年 2 月 21 日
Attached a demo .m file for your reference. Download and run the demo.
You may need to modify it to serve your purpose.
function Demo(src,event)
app.figure = uifigure;
app.edit = uieditfield(app.figure,'ValueChangedFcn',@checkValue);
function checkValue(src,event)
unit = get(src,'Value');
if isempty(unit)
uialert(app.figure,'Please enter a non-zero number, it is empty now','Warning','Icon','warning');
elseif str2double(unit)==0
uialert(app.figure,'Please enter a non-zero number, it is zero now','Warning','Icon','warning');
end
end
end
Mayvel Reese
Mayvel Reese 2022 年 2 月 22 日
Hi, thanks for this! I did it in the app designer's code view after modifying your codes but it still did not work (It only works in the command window section). Is there any way to do it in the app designer's code view section?
Simon Chan
Simon Chan 2022 年 2 月 22 日
Noticed that default name used in app designer is different from my previous code. So what you need to do is to modify the name to make it work. Attached is the demo running in app designer.
Mayvel Reese
Mayvel Reese 2022 年 2 月 22 日
It's now working. Thanks for your help!
Simon Chan
Simon Chan 2022 年 2 月 22 日
You may accept the answer if you find it useful. Thank you.

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

カテゴリ

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

質問済み:

2022 年 2 月 21 日

コメント済み:

2022 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by