10 views (last 30 days)
Show older comments
Mohd Aaquib Khan on 20 Sep 2023
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro
Commented: Mohd Aaquib Khan on 21 Sep 2023
Accepted Answer: Adam Danz
This is a continuation to Adam Danz's answer to:
While I get the plot by copyobj(), my app.uifigure is changed from app main window to the new window.
I can close the new app.uifigure, close(app.UIFigure) but the old one is still lost. So I cannot use some button down functions!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Adam Danz on 20 Sep 2023
Edited: Adam Danz on 21 Sep 2023
Open in MATLAB Online
@Mohd Aaquib Khan,
the first two lines of my answer in the thread you linked to were just to simulate app desgner. These two lines, copied below, can be removed from your implementation.
app.UIFigure = uifigure();
app.UIAxes = uiaxes(app.UIFigure);
The code in the red box in the image in your question should appear like this
- I've removed the first two lines
- I've changes app.UIAxes to app.UIaxes2 since I think this the variable that stores your axes.
% Plot cfit object externally.
a = linspace(0,10,10)';
b = linspace(0,20,10)';
[fitline,gof] = fit(a, b, 'poly1');
fig = figure('Visible','off'); % you may want to set this to "on" to see what's happening
ax = axes(fig);
h = plot(fitline,a,b);
% Copy content of temp axes to app designer
hApp = copyobj(h, app.UIAxes2);
% You'll need to recreate the legend since it isn't independently copied
lh = legend(app.UIAxes2);
% Delete temp figure
delete(fig)
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Mohd Aaquib Khan on 21 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2894587
Great, everything works well here except my name tag that you used 😄
you are right, there is no need to create a UIFigure!! My mistake for doing it without thinking.
One last doubt is do we have flexibility of adding more plots to this plot through more copyobj() in other callback functions by using hold on or by using other ways?
Adam Danz on 21 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2895432
Open in MATLAB Online
Ha! Sorry, I updated the tag.
If I understood your follow-up question correctly, you can add as many graphics objects you want by following this template
ax = axes(fig);
hold(ax,'on')
h1 = _____
h2 = _____
h3 = _____
% Copy content of temp axes to app designer
hApp = copyobj([h1,h2,h3] app.UIAxes2);
However, it's much better to directly plot to your app axes by specifying the parent handle as the first option input:
plot(app.UIAxes2,____)
The reason you can't do this with fitline is because it uses a different version of plot() that does not support the optional parent argument.
Mohd Aaquib Khan on 21 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2895557
Dont mind the tag, I just meant it as a joke. Crazy enough, the tag you used was my brothers name so I was confused if I logged in from my brothers account!! 😂
Thanks for your solution.
Sign in to comment.
More Answers (1)
dpb on 20 Sep 2023
Edited: dpb on 20 Sep 2023
Open in MATLAB Online
If the AppDesigner app main figure were named just UIFigure in the auto-generated startup code function createComponents(app), then when you execute
app.UIFigure = uifigure();
you've overwritten the application app struct UIFigure handle with the new one and (as you've discovered) orphaned the original. DON'T DO THAT!!! :) You must use a different struct name when you create the second figure.
app.UIFigure2 = uifigure();
would work.
It's surprising the original app uifigure would not have some additional decoration in its name based on the name you gave the app when you created/saved it, but it appears must be given the symptoms you describe.
Show us what the beginning of the auto-generated createComponents(app) function looks like...
9 Comments Show 7 older commentsHide 7 older comments
Show 7 older commentsHide 7 older comments
Mohd Aaquib Khan on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893697
Open in MATLAB Online
Thank you for your answer.
Below is the createComponents(app):
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 1390 752];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.ButtonDownFcn = createCallbackFcn(app, @UIFigureButtonDown, true);
app.UIFigure.Scrollable = 'on';
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Title')
Also, I have tried app.UIFigure2 = uifigure(); but I get the error below.!!
Unrecognized property 'UIFigure2' for class 'MyApp_16Sep'.
dpb on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893732
Open in MATLAB Online
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
...
So, indeed, the main app figure is just UIFigure so you did overwrite that struct value when you created the second one. That clearly is a no-no; you never want to overwrite the created component handles; once you do that, they're lost forever unless you do a findobj search to find them again.
As for the second uifigure, try
app.UIFigure2=uifigure('Name','Figure 2','Syle','modal');
to differentiate it from the first...I don't know if there are any specific things done to prevent 'normal'secondary figures in AppDesigner or not; if this doesn't work, as per ususal, we would need to see the specific code in context.
I know you could create the secondary handle as a local variable in the callback function; doing that would, of course, mean you would have to do everything that was to be done within that function prior to leaving or that handle would also be orphaned.
Mohd Aaquib Khan on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893807
Open in MATLAB Online
Still getting error with app.UIFigure2=uifigure('Name','Figure 2','Syle','modal');
Error using matlab.ui.internal.uifigureImpl
Can't find Syle property on the Figure class.
Error in uifigure (line 26)
window = matlab.ui.internal.uifigureImpl(varargin{:});
Error in myApp_16Sep/energyCycleNumberDropDownValueChanged (line 479)
app.UIFigure2=uifigure('Name','Figure 2','Syle','modal');
Anyway as you might have noticed, I was doing this to plot the fit curve as multiple plots were not possible in app till now. In 2023b we can do it I think
And I have changed the way I implement it now. I am using Y = feval( FitOutputVariable, X) and plot X,Y so it is sorted now.
So currently just pursuing it out of curiosity 😄
dpb on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893817
Open in MATLAB Online
app.UIFigure2=uifigure('Name','Figure 2','Syle','modal');
'Style' is misspelled...read things carefully!!!
Mohd Aaquib Khan on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893847
Open in MATLAB Online
Error using matlab.ui.internal.uifigureImpl
Can't find Style property on the Figure class.
Error in uifigure (line 26)
window = matlab.ui.internal.uifigureImpl(varargin{:});
Error in ITT_16Sep/energyCycleNumberDropDownValueChanged (line 479)
app.UIFigure2=uifigure('Name','Figure 2','Style','modal');
Voss on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893892
Open in MATLAB Online
WindowStyle, not Style:
app.UIFigure2=uifigure('Name','Figure 2','WindowStyle','modal');
dpb on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893897
See uifigure -- old fogey memory ain't what it used to be, it's 'WindowStyle', not just 'Style'. Again, check docs, etc.,...don't rely on blind faith.
Mohd Aaquib Khan on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893937
Yea but the bigger problem is that it is not identifying UIFigure2
Unrecognized property 'UIFigure2' for class 'MyApp_16Sep'.
dpb on 20 Sep 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2023407-app-uifigure-uifigure-creates-a-new-uifigure-window-and-the-original-app-is-not-accessible-thro#comment_2893947
Open in MATLAB Online
>> app.UIFigure=uifigure;
>> app.UIFigure2=uifigure('Name','Second','WindowStyle','modal');
>> app
app =
struct with fields:
UIFigure: [1×1 Figure]
UIFigure2: [1×1 Figure]
>> delete(app.UIFigure2)
>> delete(app.UIFigure)
>> clear app
>>
works locally so syntax isn't the problem.
Would have to see the actual MyApp_16Sep/energyCycleNumberDropDownValueChanged callback function that is where the error is...there's something amiss inside it.
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
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.
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)
- 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
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office