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

19 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Kevin Holly
Kevin Holly il 14 Apr 2022
Modificato: Kevin Holly il 14 Apr 2022
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 Commento
Joes Edders
Joes Edders il 19 Apr 2022
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by