How to link uialert to my App Designer UI figure? (2024)

48 views (last 30 days)

Show older comments

Hannes Truter on 15 Aug 2019

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure

Answered: Jonathan Wharrier on 14 Jan 2024

Open in MATLAB Online

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

Show 8 older commentsHide 8 older comments

Adam on 15 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735412

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735412

Edited: Adam on 15 Aug 2019

Open in MATLAB Online

Did you not keep hold of the class object you run for your app designer code? e.g.

myApp = MyAppDesignerProject( );

If you do then its figure should just be

myApp.UIFigure

It's always far better to just keep hold of handles to objects when you create them than try to fish them out with a huge findall net on the root node afterwards!

Hannes Truter on 15 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735424

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735424

Open in MATLAB Online

No. I do not see anything like that.

In the App Designer Code View window I see the following code that looks like it was generated by App Designer.

How to link uialert to my App Designer UI figure? (4)

In the Component Browser area of App Designer I see

How to link uialert to my App Designer UI figure? (5)

Thank you very much for your help. Your answer solved my problem for the most part.

I typed the following into the command window and it worked. The alert message appeared in front of the app UIFigure

myApp = app2( );

uialert(myApp.VTTUIFigure,{'New'},'File name error','Icon','warning','CloseFcn','uiresume(gcbf)');

Most of the second line's code comes from an answer to a different question in this forum.

https://uk.mathworks.com/matlabcentral/answers/463287-using-uialert-with-uiwait-and-uiresume

Now I just need to find the correct place to add the first line of code above to capture the class object.

Adam on 15 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735427

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735427

Yes, it is possible (and usually desirable) to change the default UIFigure name in the app so that would be the equivalent.

The first line should just be whenever your app is launched, which it must have been in order for you to be attaching a uialert to it.

Hannes Truter on 16 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735660

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735660

When I add the line "myApp = app2( );" to the code of my my app it opens a second instance of the UIFigure instead of linking to the existing figure of the app. I do not know if I'm adding it in the wrong place in the AppDesigner code. I could not find the App Designer code that creates the UIFigure. I therefore created a StartupFcn Callback as described on the MathWorks site:

https://uk.mathworks.com/help/matlab/creating_guis/app-designer-startup-function.html

I have also placed the "myApp = app2( );" code in other functions but get the same result of a second UIFigure opening.

I can not see a way of placing the "myApp = app2( );" code inside the Add Designer code without renaming the .mlapp file to .m and editing it, as described in the following link.

https://uk.mathworks.com/matlabcentral/answers/401234-app-designer-how-to-run-code-before-components-are-created

There must surely be an easier way to implement the uialert function.

In the App Designer created code I see several instances that refers to app.VTTUIFigure

% App initialization and construction

methods (Access = private)

% Create UIFigure and components

function createComponents(app)

% Create VTTUIFigure

app.VTTUIFigure = uifigure;

app.VTTUIFigure.SizeChangedFcn = createCallbackFcn(app, @VTTUIFigureSizeChanged, true);

The code above shows that app.VTTUIFigure is created by means of uifigure. This fulfills the requirement indicated on the uialert help page. When I try to use this app.VTTUIFigure figure reference in uialert it generates the error message indicated below.

uialert(app.VTTUIFigure,{'New'},'File name error','Icon','warning','CloseFcn','uiresume(gcbf)');

%Error Message: Undefined variable "app" or class "app.VTTUIFigure".

Adam on 16 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735692

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735692

Edited: Adam on 16 Aug 2019

Open in MATLAB Online

Don't put that inside your appdesigner code. You are massively over-complicating things. I guess I didn't explain it clearly enough.

myapp = app2( )

is the code that will launch app2 (assuming that is what your app designer file is called - note this is the file/class name, not whatever name is given to the uifigure inside this). That is why you get a 2nd one when you call it again.

You must already be doing that somewhere, it's likely though that you simply don't store the output argument and just call

app2

instead. Literally all you need for this is to find where you already launch the UI and keep hold of that return argument. Then you can use it later on to access its uifigure (or anything else in it that you wish).

Hannes Truter on 16 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735806

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735806

I must be doing something fundemantally wrong so I'll try to explian what I do to test my code.

There is a button on the app figure that activates a callback to the function that I'm editing. I use the MATLAB editor to edit the code of my function that calls the uialert function. I test my code by clicking the Run button in the App Designer window. I do not call my app from anywhere. The App Designer starts the app. I do not know of a way to add your code in App Designer to start the app with your code.

My latest debuging efforts:

I called the app (app2) from the command window by typing in myApp = app2( );

This did open the app. When I then clicked on the button on the app figure the code ran up to the breakpoint that I set at the point where I call uialert. When I stepped past the uialert code below, I got an error: Undefined variable "myApp" or class "myApp.VTTUIFigure".

uialert(myApp.VTTUIFigure,{'New'},'File name error','Icon','warning');

When I type both lines below in the command window without clicking on the button on the app figure, the alert window does open in front of the app figure.

myApp = app2( );

uialert(myApp.VTTUIFigure,{'New'},'File name error','Icon','warning');

The same uialert code works in the command window but not in my callback function code.

By stepping through all the code I found the place in the App Designer code where the figure is created.

How to link uialert to my App Designer UI figure? (10)

This is inside the App Designer app2.mlapp code which I can not edit.

Adam on 16 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735825

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_735825

Open in MATLAB Online

Ah, if you are running your uialert from a button within the app then you have access to the app within that button's callback, as you do in all callbacks. So

app.VTTUIFigure

should be the uifigure, in your callback. You don't need to add in the code I mentioned to keep the app handle in that case. I assumed, from what you were saying, that you were trying to call uialert externally to your app and give it the figure of your app as an argument.

Hannes Truter on 19 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_736589

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_736589

Edited: Hannes Truter on 19 Aug 2019

When I use app.VTTUIFigure in

uialert(app.VTTUIFigure,{'New'},'File name error','Icon','warning');

I still get the same error message: Undefined variable "app" or class "app.VTTUIFigure".

I used code from this MATLAB answer to track the status of VTTUIFigure. I wanted to see if I can find the point where VTTUIFigure can not be called any more.

I executed the code below next to K>>, from the command window, at different times while I stepped through the APP Designer code of the app.

K>> class(app.VTTUIFigure)

K>> isuifigure(app.VTTUIFigure)

Below is the function that is called from isuifigure() above

function val = isuifigure(h)

val = ~isempty(matlab.ui.internal.dialog.DialogHelper.getFigureID(h));

end

The APP Designer code that creates the VTTUIFigure starts executing when I step into the "runningApp" part of the code (copied below) which is inside the AppManagementService.m module.

try

runningApp = evalin('base', sprintf('%s(%s);', appName, appArguments))

catch exception

Just after the VTTUIFigure of the app was created I ran the code to check its status. Below is the code I executed in the command window as well as the responses:

K>> class(app.VTTUIFigure)

ans = 'matlab.ui.Figure'

K>> isuifigure(app.VTTUIFigure)

ans = logical 1

I executed the above code every time after stepping to the next line in the App Designer code.

Every time when the code returned from the function below to the "runningApp" code above that called the AppDesigner code, the VTTUIFigure status started giving me error messages.

% Construct app

function app = app2

% Create and configure components

createComponents(app)

% Register the app with App Designer

registerApp(app, app.VTTUIFigure)Just after the

if nargout == 0

clear app

end

end %Error status happens after stepping back from this end to runningApp = evalin('base',

Just after exit of above code, the VTTUIFigure class changes

K>> class(app.VTTUIFigure)

Undefined variable "app" or class "app.VTTUIFigure".

K>> isuifigure(app.VTTUIFigure)

Undefined variable "app" or class "app.VTTUIFigure".

When I execute the uialert() code below immediately after VTTUIFigure is created in the AppDesigner code, it creates the expected alert window and message in front of VTTUIFigure.

uialert(app.VTTUIFigure,{'New'},'File name error','Icon','warning');

I tested this by setting a breakpoint just after the AppDesigner code below, and then executing the uialert code from the command window.

% Create VTTUIFigure

app.VTTUIFigure = uifigure;

app.VTTUIFigure.Color = [0.9294 0.6902 0.1294];

app.VTTUIFigure.Colormap = [0.2431 84 0.3451 0......9765 0.9843 0.0824];

app.VTTUIFigure.Position = [100 100 507 482];

app.VTTUIFigure.Name = 'VTT';

app.VTTUIFigure.Resize = 'off';

app.VTTUIFigure.SizeChangedFcn = createCallbackFcn(app, @VTTUIFigureSizeChanged, true);

Adam on 19 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_736632

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_736632

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 on 20 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_737009

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_737009

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.

Sign in to comment.

Sign in to answer this question.

Answers (4)

Eduardo Gil on 9 Dec 2019

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_405643

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_405643

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

Show -2 older commentsHide -2 older comments

Sign in to comment.

Eduardo Gil on 10 Dec 2019

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_405660

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_405660

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

Show NoneHide None

Hannes Truter on 10 Dec 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_776448

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_776448

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 on 21 Jul 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_944802

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#comment_944802

Greetings,

I found it, try: app.UIFigure

Sign in to comment.

Jonathan Wharrier on 17 Dec 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_1372982

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_1372982

Open in MATLAB Online

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.

How to link uialert to my App Designer UI figure? (20)

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

Show -2 older commentsHide -2 older comments

Sign in to comment.

Jonathan Wharrier on 14 Jan 2024

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_1389096

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/476266-how-to-link-uialert-to-my-app-designer-ui-figure#answer_1389096

Open in MATLAB Online

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

How to link uialert to my App Designer UI figure? (22)

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...

How to link uialert to my App Designer UI figure? (23)

I took a punt that this meant that the rest of the program could not actually see the figure so I went for

How to link uialert to my App Designer UI figure? (24)

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...

How to link uialert to my App Designer UI figure? (25)

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

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABApp BuildingDevelop Apps ProgrammaticallyDevelop uifigure-Based Apps

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Tags

  • uialert
  • app designer uifigure

Products

  • MATLAB

Release

R2017b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to link uialert to my App Designer UI figure? (26)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How to link uialert to my App Designer UI figure? (2024)

References

Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6256

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.