Calling a CloseFcn with 'app' as an argument when closing uialert dialog box in App Designer
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Joes Edders
le 14 Avr 2022
Commenté : Joes Edders
le 19 Avr 2022
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.
0 commentaires
Réponse acceptée
Kevin Holly
le 14 Avr 2022
Modifié(e) : Kevin Holly
le 14 Avr 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!