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)
lampColor = app.Lamp.Color;
if isequal(lampColor, [1 0 0])
tabTitle = ['Result ' num2str(length(app.TabGroup.Children) + 1)];
newTab = uitab(app.TabGroup, 'Title', tabTitle);
app.TabGroup.SelectedTab = newTab;
newAxes = uiaxes(newTab);
app.configureAxes(newAxes);
elseif isequal(lampColor, [0 1 0])
currentTab = app.TabGroup2.SelectedTab;
existingAxes = findobj(currentTab.Children, 'Type', 'matlab.ui.control.UIAxes');
existingAxes = uiaxes(currentTab);
app.configureAxes(existingAxes);
currentAxes = existingAxes;
uialert(app.UIFigure, 'Unrecognized lamp color.', 'Warning');
x = linspace(0, 10, 100);
function configureAxes(app,ax)
xlabel(ax, 'Wavelength (um)')
ax.FontName = 'Garamond';
ax.ColorOrder = [0 0.4471 0.7412;
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)
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.