Empty plot window pop up with App Designer

Hello,
I am a beginner using App Designer. I just finished coding a graphic interface, everything is going fine except that an empty figure popped up when I pushed the button to plot on the UIAxes of the interface.
I have already read some questions asked about this topic, but I have checked my code, and each time I'm plotting I am using app.UIAxes so the problem seems not to be exactly the same.
I also try to implement the following code :
fig1 = figure();
set(fig1, 'Visible', 'off'); % removes the default figure pop-up during live plot.
But it is not working. The purpose of the application is to calculate and plot the polynomial interpolation of data measurements. The same type of code is repeated for the different degrees of interpolation buttons. And for each button, the problem occurs. However, if I pushed the button for a degree and then to another, the empty figure popped up at the first action but not at the second one.
Please find below a part of my Matlab code :
if app.AcqButton==0
f = errordlg('Lauch Acquisition first !', 'Acquisition Error');
else
P3 = polyfit(app.N,app.x,3); % calcul de l'approximation polynomiale de degré 3
yfit = polyval(P3,app.N); %calcul les valeurs de l'approximation polynomiale pour chaque abscisse
cla(app.UIAxes);
cla(app.UITable);
CN=strings(1, app.InterpolDeg+1);
for k=1:4
CN(1,k)=strcat('a', num2str(k));
end
app.UITable.Data=P3;
app.UITable.ColumnName=CN;
fig1 = figure();
set(fig1, 'Visible', 'off'); % removes the default figure pop-up during live plot.
hold( app.UIAxes, 'on' ) %allows you to plot everything on the same figure without overwriting the curves
scatter(app.UIAxes,app.N,app.x,'blue');
plot(app.UIAxes,app.N,yfit,'r-.');
eqn = string(" Polynomial 3: y = " + P3(1)) + "x**3 + " + string(P3(2)) + "x**2 +" +string(P3(3))+ "x +" + string(P3(4));
text(app.UIAxes, min(app.N),max(app.x),eqn,"HorizontalAlignment","left","VerticalAlignment","middle")
%hold( app.UIAxes, 'off' )
end
Thank you by advance for any help !

 Accepted Answer

Your problem is that you are apparently initialising a new empty figure for no reason.
Try to remove these two lines:
fig1 = figure();
set(fig1, 'Visible', 'off');
When you specify the axis of the plot with:
scatter(app.UIAxes, app.N, app.x, 'blue');
plot(app.UIAxes, app.N, yfit, 'r-.');
You are most probably referring to some already existing axis in the app.UIFigure of your application.
By doing fig1 = figure() you create a new figure but then you plot in the UIAxes of your application's UIFigure.
Moreover pay attention that UIAxes are Children of UIFigures, while Axes are Children of Figures.
These are two different things!
If you want your plot to appear in a new pop-up window you can do the following:
fig1 = figure;
ax = axes(fig1);
scatter(ax, app.N, app.x, 'blue');
plot(ax, app.N, yfit, 'r-.');
Otherwise just remove those two lines where you define your figure.
As a side-tip, you can initialise your figure using:
fig1 = figure('Visibile','Off');
If this doesn't solve your problem, try to share your code to have more insightful help.

7 Comments

Hello,
Thank you for your reply. I had just added these two lines of code to remove the unwanted figure, I just deleted them and restarted and the empty plot window is still there.
You will find the whole code below, my apologies it's a bit long. Some of it was automatically generated by App Designer.
classdef interface_V1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CalculButton matlab.ui.control.Button
DegreeoftheinterpolationSpinner matlab.ui.control.Spinner
DegreeoftheinterpolationSpinnerLabel matlab.ui.control.Label
Polynomial3Button matlab.ui.control.Button
Polynomial2Button matlab.ui.control.Button
Polynomial1Button matlab.ui.control.Button
PlotdataButton matlab.ui.control.Button
UITable matlab.ui.control.Table
AcquisitionButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
AcqButton=0; % Description
N; %abscissa and ordinate are passed as global
x; % parameters so that they can be reused between functions
CalcButton=0;
InterpolDeg=0;
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: AcquisitionButton
function AcquisitionButtonPushed(app, event)
%%Acquisition of experimental data%%
file = uigetfile; %retrieve the file containing the data to be acquired
figure(app.UIFigure);
%app.UIFigure.Visible = 'on'; %solve the problem of the app going background
L = readmatrix(file);
app.N = L(:,1); %abscissa: number of the measurement point
app.x = L(:,2); %ordinate: associated measure
app.AcqButton=1;
end
% Button pushed function: PlotdataButton
function PlotdataButtonPushed(app, event)
if app.AcqButton==1
scatter(app.UIAxes,app.N,app.x);
else
f = errordlg('Lauch Acquisition first !', 'Acquisition Error');
end
end
% Button pushed function: Polynomial1Button
function Polynomial1ButtonPushed(app, event)
if app.AcqButton==0
f = errordlg('Lauch Acquisition first !', 'Acquisition Error');
else
P1 = polyfit(app.N,app.x,1); % calculation of the polynomial approximation of degree 1
yfit = polyval(P1,app.N); % calculates the values of the polynomial approximation for each abscissa
cla(app.UIAxes);
cla(app.UITable);
CN=strings(1, app.InterpolDeg+1);
for k=1:2
CN(1,k)=strcat('a', num2str(k));
end
app.UITable.Data=P1;
app.UITable.ColumnName=CN;
hold( app.UIAxes, 'on' ) %allows you to plot everything on the same figure without overwriting the curves
scatter(app.UIAxes,app.N,app.x,'blue');
plot(app.UIAxes,app.N,yfit,'r-.');
eqn = string(" Linear: y = " + P1(1)) + "x + " + string(P1(2)); %affichage de l'équation
text(app.UIAxes, min(app.N),max(app.x),eqn,"HorizontalAlignment","left","VerticalAlignment","middle")
%hold( app.UIAxes, 'off' )
end
end
% Button pushed function: Polynomial2Button
function Polynomial2ButtonPushed(app, event)
if app.AcqButton==0
f = errordlg('Lauch Acquisition first !', 'Acquisition Error');
else
P2 = polyfit(app.N,app.x,2); % calcul de l'approximation polynomiale de degré 2
yfit = polyval(P2,app.N); %calcul les valeurs de l'approximation polynomiale pour chaque abscisse
cla(app.UIAxes);
cla(app.UITable);
CN=strings(1, app.InterpolDeg+1);
for k=1:3
CN(1,k)=strcat('a', num2str(k));
end
app.UITable.Data=P2;
app.UITable.ColumnName=CN;
hold( app.UIAxes, 'on' ) %allows you to plot everything on the same figure without overwriting the curves
scatter(app.UIAxes,app.N,app.x,'blue');
plot(app.UIAxes,app.N,yfit,'r-.');
eqn = string(" Polynomial 2: y = " + P2(1)) + "x**2 + " + string(P2(2)) + "x +" +string(P2(3));
text(app.UIAxes, min(app.N),max(app.x),eqn,"HorizontalAlignment","left","VerticalAlignment","middle")
%hold( app.UIAxes, 'off' )
end
end
% Button pushed function: Polynomial3Button
function Polynomial3ButtonPushed(app, event)
if app.AcqButton==0
f = errordlg('Lauch Acquisition first !', 'Acquisition Error');
else
P3 = polyfit(app.N,app.x,3); % calcul de l'approximation polynomiale de degré 3
yfit = polyval(P3,app.N); %calcul les valeurs de l'approximation polynomiale pour chaque abscisse
cla(app.UIAxes);
cla(app.UITable);
CN=strings(1, app.InterpolDeg+1);
for k=1:4
CN(1,k)=strcat('a', num2str(k));
end
app.UITable.Data=P3;
app.UITable.ColumnName=CN;
hold( app.UIAxes, 'on' ) %allows you to plot everything on the same figure without overwriting the curves
scatter(app.UIAxes,app.N,app.x,'blue');
plot(app.UIAxes,app.N,yfit,'r-.');
eqn = string(" Polynomial 3: y = " + P3(1)) + "x**3 + " + string(P3(2)) + "x**2 +" +string(P3(3))+ "x +" + string(P3(4));
text(app.UIAxes, min(app.N),max(app.x),eqn,"HorizontalAlignment","left","VerticalAlignment","middle")
%hold( app.UIAxes, 'off' )
end
end
% Button pushed function: CalculButton
function CalculButtonPushed(app, event)
app.CalcButton=1;
if app.InterpolDeg==0
g = errordlg('Degree must be greater than 0!', 'Degree Error');
end
if app.AcqButton==0
f = errordlg('Lauch Acquisition first !', 'Acquisition Error');
end
cla(app.UIAxes);
cla(app.UITable);
CN=strings(1, app.InterpolDeg+1);
for k=1:app.InterpolDeg+1
CN(1,k)=strcat('a', num2str(k));
end
P=zeros([1 app.InterpolDeg+1]); %on définit un tableau vide qui va contenir les coefficients d'approximation polynomiale
for q=1:app.InterpolDeg+1
if q<=length(polyfit(app.N,app.x,app.InterpolDeg))
var=polyfit(app.N,app.x,app.InterpolDeg);
P(1,q)=var(q);
end
end
app.UITable.Data=P;
app.UITable.ColumnName=CN;
hold( app.UIAxes, 'on' ) %allows you to plot everything on the same figure without overwriting the curves
scatter(app.UIAxes,app.N,app.x,'blue');
yfit=polyval(P(1,1:app.InterpolDeg+1),app.N);
plot(app.UIAxes,app.N, yfit);
%hold( app.UIAxes, 'off' )
end
% Value changed function: DegreeoftheinterpolationSpinner
function DegreeoftheinterpolationSpinnerValueChanged(app, event)
app.InterpolDeg = app.DegreeoftheinterpolationSpinner.Value;
end
% Display data changed function: UITable
function UITableDisplayDataChanged(app, event)
end
end
% 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 756 583];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Function')
xlabel(app.UIAxes, 'N')
ylabel(app.UIAxes, 'x')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [417 268 300 305];
% Create AcquisitionButton
app.AcquisitionButton = uibutton(app.UIFigure, 'push');
app.AcquisitionButton.ButtonPushedFcn = createCallbackFcn(app, @AcquisitionButtonPushed, true);
app.AcquisitionButton.Position = [161 502 100 23];
app.AcquisitionButton.Text = 'Acquisition';
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
app.UITable.RowName = {};
app.UITable.DisplayDataChangedFcn = createCallbackFcn(app, @UITableDisplayDataChanged, true);
app.UITable.Position = [81 128 525 59];
% Create PlotdataButton
app.PlotdataButton = uibutton(app.UIFigure, 'push');
app.PlotdataButton.ButtonPushedFcn = createCallbackFcn(app, @PlotdataButtonPushed, true);
app.PlotdataButton.Position = [161 442 100 23];
app.PlotdataButton.Text = 'Plot data';
% Create Polynomial1Button
app.Polynomial1Button = uibutton(app.UIFigure, 'push');
app.Polynomial1Button.ButtonPushedFcn = createCallbackFcn(app, @Polynomial1ButtonPushed, true);
app.Polynomial1Button.Position = [22 370 100 23];
app.Polynomial1Button.Text = 'Polynomial 1°';
% Create Polynomial2Button
app.Polynomial2Button = uibutton(app.UIFigure, 'push');
app.Polynomial2Button.ButtonPushedFcn = createCallbackFcn(app, @Polynomial2ButtonPushed, true);
app.Polynomial2Button.Position = [161 370 100 23];
app.Polynomial2Button.Text = 'Polynomial 2°';
% Create Polynomial3Button
app.Polynomial3Button = uibutton(app.UIFigure, 'push');
app.Polynomial3Button.ButtonPushedFcn = createCallbackFcn(app, @Polynomial3ButtonPushed, true);
app.Polynomial3Button.Position = [294 370 100 23];
app.Polynomial3Button.Text = 'Polynomial 3°';
% Create DegreeoftheinterpolationSpinnerLabel
app.DegreeoftheinterpolationSpinnerLabel = uilabel(app.UIFigure);
app.DegreeoftheinterpolationSpinnerLabel.HorizontalAlignment = 'right';
app.DegreeoftheinterpolationSpinnerLabel.Position = [60 306 147 22];
app.DegreeoftheinterpolationSpinnerLabel.Text = 'Degree of the interpolation';
% Create DegreeoftheinterpolationSpinner
app.DegreeoftheinterpolationSpinner = uispinner(app.UIFigure);
app.DegreeoftheinterpolationSpinner.ValueChangedFcn = createCallbackFcn(app, @DegreeoftheinterpolationSpinnerValueChanged, true);
app.DegreeoftheinterpolationSpinner.Position = [222 306 100 22];
% Create CalculButton
app.CalculButton = uibutton(app.UIFigure, 'push');
app.CalculButton.ButtonPushedFcn = createCallbackFcn(app, @CalculButtonPushed, true);
app.CalculButton.Position = [195 267 100 23];
app.CalculButton.Text = 'Calcul';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = interface_V1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Problem
The problem comes from the command:
cla(app.UITable);
In the functions Polynomial1ButtonPushed, Polynomial2ButtonPushed, Polynomial3ButtonPushed and CalculButtonPushed.
The commmand cla clear the content (Children) of an axes object, but if no axes is specified as input, cla executes on gca.
gca is the handle to the current axes and that is why when you use plot you don't have to specify the axes. Matlab uses gca automatically.
Nontheless gca refer to axes objects, not uiaxes. Again, this is why when you plot in uiaxes you have to specify the uiaxes handle with:
plot(app.UIAxes, x ,y);
Because you are using uiaxes there is no current axes and when gca can't return any axes, it creates a new one, thus making a new figure popping up.
You can try this by typing in your command window:
gca
Solution
The way to solve this is to use:
app.UITable.Data = {};
Instead of:
cla(app.UITable);
And the window won't pop up.
Moreover, because you overwrite the table Data a few lines below, you don't even need to clear the UITable content and you can just delete every occurance of this line:
cla(app.UITable);
Without replacing it with anything else.
This is the approach I recommend you.
You could do the same with
cla(app.UIAxes);
By playing instead with:
hold(app.UIAxes,'On')
hold(app.UIAxes,'Off')
But this is not necessary.
Also this line:
figure(app.UIFigure);
In the function AcquisitionButtonPushed is conceptually wrong and even if it has no effect I suggest you to delete it.
Thanks a lot, it works now!
The command
figure(app.UIFigure);
is used to prevent the app from running in the background behind other open windows. This seems to be a recurring bug in App Designer. May you know a more elegant solution to this problem?
Do you mean that is used to start the app as active window in front of the others?
If so I don't recall ever having this problem.
Do you remember where did you read to use this?
When I click on the "Acquire" button, I can choose the file containing the data in the directory of my computer, but once imported the window containing the application is put in the background of all the other windows on my desktop.
Here is the link to the question asked and the answer I got, which solves the problem
Okay now I understand and remeber having the same issue with uigetfile once.
To my knowledge, there is no elegant solution and you have already choosen my favourite among the ones listed in that question, so just ignore my previous advice.
Very nice, thank you very much for your valuable advices ! Have a nice day !

Sign in to comment.

More Answers (1)

I also had a smiliar issue,
I realized that I had left Hold on /off between the function that was updating my axes.

Categories

Find more on App Building in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 28 Apr 2023

Answered:

on 23 Feb 2024

Community Treasure Hunt

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

Start Hunting!