Calling a CloseFcn with 'app' as an argument when closing uialert dialog box in App Designer

28 ビュー (過去 30 日間)
Hello,
I am creating a GUI and I wish to be able to prevent the user from inputting certain things in certain edit fields. I have made use of the 'uialert()' function when I catch an invalid input. The following code is part of the function which is called when the Initial CO2 edit field is changed:
if(app.InitialCO2LevelEditField.Value / 100 > app.InitialInternalPressureEditField.Value)
uialert(app.UIFigure,'ERROR: CO2 partial pressure is higher than the internal pressure.','Input Error')
end
This creates an error message warning the user that the value they have just input an invalid value. Elsewhere in the code, I have a function which stores the values in edit fields in a variable. What I wish to do now is to replace the invalid value which has just been input with the stored (and valid) value in this variable.
To do this I have a function 'Restore_Values(app)' which I can call to restore values in the edit fields. I want this function to be called upon closing the dialog window created by the above code. I know that the 'uialert()' function allows for function handles and arguments to be passed to it for a CloseFcn but I can't get it to recognise the 'app' variable which is needed to access the UIFigure.
I have tried:
if(app.InitialCO2LevelEditField.Value / 100 > app.InitialInternalPressureEditField.Value)
uialert(app.UIFigure,'ERROR: CO2 partial pressure is higher than the internal pressure.','Input Error','Restore_Values(app)')
end
Which gives me the error:
Warning: Error while evaluating 'CloseFcn' for dialog.
Error using evalin
Unrecognized function or variable 'app'.
> In matlab.ui.internal.dialog/DialogController/closeCallback (line 53)
In matlab.ui.internal.dialog.AlertDialogController>@(evd)this.closeCallback(evd) (line 37)
In message.internal/MessageService/doExecuteCallback
In message.internal/MessageService/executeCallback
In message.internal.executeCallback
I have also tried:
if(app.InitialCO2LevelEditField.Value / 100 > app.InitialInternalPressureEditField.Value)
uialert(app.UIFigure,'ERROR: CO2 partial pressure is higher than the internal pressure.','Input Error',{'Restore_Values','app'})
end
and
if(app.InitialCO2LevelEditField.Value / 100 > app.InitialInternalPressureEditField.Value)
uialert(app.UIFigure,'ERROR: CO2 partial pressure is higher than the internal pressure.','Input Error',{'Restore_Values',app})
end
Both of which give me the error:
Warning: Error while evaluationg 'CloseFcn' for dialog.
Error using feval.
Invalid function name 'Restore_Values(app)'.
> In matlab.ui.internal.dialog/DialogController/closeCallback (line 53)
In matlab.ui.internal.dialog.AlertDialogController>@(evd)this.closeCallback(evd) (line 37)
In message.internal/MessageService/doExecuteCallback
In message.internal/MessageService/executeCallback
In message.internal.executeCallback
So I feel like I'm nearly there. Any advice on how to call a function which can access all the data within the UI would be greatly received.

採用された回答

Kevin Holly
Kevin Holly 2022 年 4 月 14 日
編集済み: Kevin Holly 2022 年 4 月 14 日
Joes,
Please see the app attached. You need to place the Restore_Values function within the callback function used to execute the uialert command. In the example attached, it would be the pushbutton's callback. You may have it in the editfield's callback.
  1 件のコメント
Joes Edders
Joes Edders 2022 年 4 月 19 日
Apologies for the late response but it was a holiday weekend here.
Thank you for the answer and for the example app. Both were very helpful.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by