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

조회 수: 20 (최근 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 CenterFile Exchange에서 Develop 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