How to link uialert to my App Designer UI figure?
42 views (last 30 days)
Show older comments
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 Comments
Adam
on 19 Aug 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.
Answers (4)
Eduardo Gil
on 9 Dec 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)
0 Comments
Eduardo Gil
on 10 Dec 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 Comments
Jonathan Wharrier
on 17 Dec 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...
0 Comments
Jonathan Wharrier
on 14 Jan 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.
0 Comments
See Also
Categories
Find more on Develop uifigure-Based Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!