How to plot new results only in tab that is being viewed by user - App Designer

9 次查看(过去 30 天)
Within my app I am attempting to have the simulation results be plotted in a new plot that is within a new tab. I have been succesful in generating a new tab and plot within that tab. I have also been succesful in getting the results to plot in that new plot/tab. However, I have not been able to realize the functionality that I set out to do.
Specifically I want to be able to plot the results in the figure within the tab that is be looked at by the user. If the lamp is green it will overwrite the plot on the tab that is being looked at when I press 'Execute' (and not create a new tab/plot or plot on a tab/plot that is not being viewed). If the lamp is red then they are generating a new result plot in a new tab when 'Execute' is pressed. How can this be done?
I am looking to generate these plots in such a way that I can have access to them (i.e. thier handles) later to be saved and exported.
Other information that might be useful is that the 'TabGroup' is within a panel and the way that I am getting coding alerts with how I am executing my plot creation and then using the object. The error is "Specify a UIAxes handle as first argument". This error does not seem to seem to interupt my app functionality, but may be a source of my diffculties.
Here is the code for the beginning of Execute button in MATLAB App Designer followed by a screen shot of the section of the app. Any tips or suggestions will be greatly appreciated.
function ExecuteButtonPushed(app, event)
% Start Clock
tic;
% Plot Results on New Tab
NewOutput = app.Lamp_5.Color;
if NewOutput == [1.00,0.00,0.00]
tab = uitab(app.TabGroup2,"Title",['Result ' num2str((length(app.TabGroup2.Children))+1)]);
app.TabGroup2.SelectedTab = tab;
NewPlot = uiaxes(tab);
title(NewPlot, 'Spectra')
xlabel(NewPlot, 'Wavelength (um)')
ylabel(NewPlot, 'Amplitude')
NewPlot.FontName = 'Garamond';
NewPlot.FontSize = 14;
NewPlot.FontWeight = 'bold';
NewPlot.ColorOrder = [0 0.4471 0.7412;0.851 0.3294 0.102;0.9294 0.6941 0.1255;0.4941 0.1843 0.5569;0.4667 0.6745 0.1882;0.302 0.7451 0.9333;0.6353 0.0784 0.1843];
NewPlot.XGrid = 'on';
NewPlot.YGrid = 'on';
NewPlot.Position = [0 1 554 395];
CurrentPlot = NewPlot;
end
if NewOutput == [0.00,1.00,0.00]
CurrentPlot = app.UIAxes_2;
end

回答(1 个)

Abhipsa
Abhipsa 2025-5-30
I believe that you want to achieve the following goals:
  • If the lamp is red, then you want to create a new tab and plot in it.
  • If the lamp is green, plot directly into the currently selected tab, do not add a new tab or switch tabs and reuse or create a “UIAxes” in that tab for plotting.
  • You want to keep access to the plot handles for later export.
  • Resolution for the warning message “Specify a UIAxes handle as first argument.”
The plots are being created on a new figure window instead of the tabs. This issue can be resolved by passing the required axis as an argument to the “plot” function.
You can refer to the following MATLAB documentation for more information:
I am setting up a dummy app as shown below to answer the query:
I have modified your provided code as following to achieve the requirements:
function ExecuteButtonPushed(app, event)
% Start Clock
tic;
lampColor = app.Lamp.Color;
if isequal(lampColor, [1 0 0]) % RED LAMP → Create New Tab
tabTitle = ['Result ' num2str(length(app.TabGroup.Children) + 1)];
newTab = uitab(app.TabGroup, 'Title', tabTitle);
app.TabGroup.SelectedTab = newTab;
newAxes = uiaxes(newTab);
app.configureAxes(newAxes);
currentAxes = newAxes;
elseif isequal(lampColor, [0 1 0]) % GREEN LAMP → Use Existing Tab
currentTab = app.TabGroup2.SelectedTab;
% Check if UIAxes already exists
existingAxes = findobj(currentTab.Children, 'Type', 'matlab.ui.control.UIAxes');
if isempty(existingAxes)
existingAxes = uiaxes(currentTab);
app.configureAxes(existingAxes);
end
currentAxes = existingAxes;
else
uialert(app.UIFigure, 'Unrecognized lamp color.', 'Warning');
return;
end
% Plot simulation results (example data)
x = linspace(0, 10, 100);
y = sin(x);
plot(currentAxes, x, y);
end
function configureAxes(app,ax) %we need to pass the app as a parameter else error will be thrown
title(ax, 'Spectra')
xlabel(ax, 'Wavelength (um)')
ylabel(ax, 'Amplitude')
ax.FontName = 'Garamond';
ax.FontSize = 14;
ax.FontWeight = 'bold';
ax.ColorOrder = [0 0.4471 0.7412;
0.851 0.3294 0.102;
0.9294 0.6941 0.1255;
0.4941 0.1843 0.5569;
0.4667 0.6745 0.1882;
0.302 0.7451 0.9333;
0.6353 0.0784 0.1843];
ax.XGrid = 'on';
ax.YGrid = 'on';
end
Output of the MLApp when lamp is green:
Output of the MLApp when lamp is red, we can see that a new tab named “Result 3” is getting created:
To store the handle, a property in the app can be added
properties (Access = private)
AllPlotAxes = {}; % Cell array to store UIAxes handles
end
The below line can be added after adding a new axes to the handle
app.AllPlotAxes{end+1} = currentAxes;
The warning message “Specify a UIAxes handle as first argument.” Shows up because of the warning is triggered by App Designer's code analysis when it does not detect a “UIAxes” handle as the first argument in plotting functions.
To resolve this warning message, ensure that the “UIAxes” handle is correctly specified.
I hope this resolves your query.

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by