How to link uialert to my App Designer UI figure?

43 visualizaciones (últimos 30 días)
Hannes Truter
Hannes Truter el 15 de Ag. de 2019
Respondida: Jonathan Wharrier el 14 de En. de 2024
I recently took over a MATLAB project of a former colleague and is still learning how to write MATLAB code correctly. I would like to change working code that currently uses msgbox to now rather make use of uialert(). I can however not find a way to link the UIFigure of my app to the uialert function. My understanding is that the arguments should be as follows: uialert(uifigure,message,title). My problem seems to be with the uifigure argument.
The uialert Help indicates that the uifigure must be created with the uifigure function. When I use the following code I do get a new uifigure and the alert message appears in front of it as expected.
f = uifigure;
uialert(f,'Test','warning');
I do however want the alert message to appear in front of the App Designer UIFigure rather than a newly created figure. I found code here on the forum that identifies the name of my App Designer figure:
hFigs = findall(groot, 'Type', 'figure')
hFigs = Figure (VTT) with properties:
Number: []
Name: 'VTT'
I tried several ways to try to refer to the name of my App Designer figure but none of them worked. Below are two examples of what I tried.
uialert('VTT','Test','warning');
uialert(hFigs.Name,'Test','warning');
I got the following error with both lines of code:
Error using uialert (line 35)
Invalid or deleted figure handle. First argument must be a valid figure handle
I might be using the wrong terminology but I can not find any answers in this forum that explained to me how I can link uialert() to appear in front of the the App Designer UIFigure.
  10 comentarios
Adam
Adam el 19 de Ag. de 2019
As long as you are in a callback of your application 'app' should never not exist (assuminig the argument is called 'app'. From my experience there is no way of changing the name of this argument so I assume it is). And if the app exists then so should its uifigure property which you named VTTUIFigure.
You shouldn't have to be messing around with any code that constructs the app or creates the figure.
Hannes Truter
Hannes Truter el 20 de Ag. de 2019
Everything you wrote above seems logical but the reality does not seem to match it in my app. I can not spend any more time on this. I'll just make peace with the fact that uialert can not work with the GUI of my app. I can not find a single example on any of the forums I looked at where anybody successfully implemented uialert with their uifigure. I'll just keep on using the message box.

Iniciar sesión para comentar.

Respuestas (4)

Eduardo Gil
Eduardo Gil el 9 de Dic. de 2019
I am having the same problem. My code is very straightforward - just an error message to the user in case "end date" is less than "start date".
% error message in case end_date <= start_date
d=app.UIAxes;
if end_date <= start_date
uialert(d,'Plot End Date must be greater than Plot Start Date!','Invalid Input');
end
I get the following error:
Error using uialert (line 42)
Invalid or deleted figure handle. First argument must be a valid figure handle
Error in Weather_Chart/refreshplot (line 214)
plot_data(app,xdata,ydata,zdata,zdata_backcast,min_date,max_date);
Error in Weather_Chart/DatePicker_2ValueChanged (line 266)
refreshplot(app)

Eduardo Gil
Eduardo Gil el 10 de Dic. de 2019
Found the aswer. It's not app.UIAxes, but rather the name for the entire figure (in my case: Load_Analysis matlab.ui.Figure). A bit of trial and error:
uialert(App.Load_Analysis,'Plot End Date must be greater than Plot Start Date!','Invalid Input');
  2 comentarios
Hannes Truter
Hannes Truter el 10 de Dic. de 2019
Thank you for providing the solution you found for your problem.
Are you using uialert with a secondary figure you created with your app code or are you using it with the main app figure created on the "Design View" tab in App Designer?
My problem is specifically on how to use uialert with the main figure created in App Designer. I only have the one figure. I do not create any secondary figures in the app code. It looks to me like there is fixed code in the % Create UIFigure section of the App Designer startup code that disables the uialert functionality with the main app figure.
Frederick Breidt
Frederick Breidt el 21 de Jul. de 2020
Greetings,
I found it, try: app.UIFigure

Iniciar sesión para comentar.


Jonathan Wharrier
Jonathan Wharrier el 17 de Dic. de 2023
I am also having a problem with this. Clearly the code
function ErrorMessageDisplay(~,message,header,icon)
fig = uifigure;
uialert(fig,message,header,"Icon",icon)
end
within my app works but produces a result which is unacceptable.
This is because the figure is a new one and not the app itself. If one looks at the code of the app there is a line which creates the app uifigure right at the beginning of the creation code
app.NLSEAppDevGPU1181UIFigure = uifigure('Visible', 'off');
later on a line appears that makes the whole thing visible which is
app.NLSEAppDevGPU1181UIFigure.Visible = 'on';
at which point it seemed that maybe
fig = app.NLSEAppDevGPU1181UIFigure;
should be the answer to which I get the error...
Unable to resolve the name 'app.NLSEAppDevGPU1181UIFigure'.
so in spite of it creating a UIFigure of that name, apparently that UI figure does not exist...

Jonathan Wharrier
Jonathan Wharrier el 14 de En. de 2024
I have struggled with this but I think I finally have an (not the) answer!! I have found that it turns out to be quite simple. I just was looking at the problem the wrong way. For the command uialert you need a figure. All apps have one.
Here is mine.
properties (Access = public)
UIFigure matlab.ui.Figure
TabGroup matlab.ui.container.TabGroup
SetUpTab matlab.ui.container.Tab
so, of course I write a UI alert, I put it in a start up function
function DisplayErrorMessage(~,msg,title,icon)
uialert(app.UIFigure,msg,title,"Icon",icon);
end
and I got
By complete chance I was browsing the part of the code that I cannot change because I thought maybe there is a reason that it cannot find the figure that clearly the app contains.
BEFORE:
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Color = [0 0 1];
app.UIFigure.Position = [100 100 1310 659];
app.UIFigure.Name = 'NLSE App v1.19';
So I called it up in the figure in the component browser and basically clicked on anything!! (Great advice from my supervisor, when in doubt click on everything!) when I came across...
I took a punt that this meant that the rest of the program could not actually see the figure so I went for
and immediately a flicker in the code next to the browser showed me
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Color = [0 0 1];
app.UIFigure.Position = [100 100 1310 659];
app.UIFigure.Name = 'NLSE App v1.19';
app.UIFigure.HandleVisibility = 'on';
at which point clicking happily away on the run button I obtained.... the same failure as before. I tried to be clever and create my own error message subroutine that just received the error message and called uialert inside the routine. It fails giving the same code. However I did find that if I made calls direct to uialert it produced...
Joy unrestrained... well not quite. This was generated inside an update button in the program by a direct call to uialert. So in my catch statement I need a direct call apparently. Not sure I understand why it does not work in the subroutine but at least we are moving on.

Categorías

Más información sobre Develop uifigure-Based Apps en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by