Set current axes by using axes() is not wokring
31 views (last 30 days)
Show older comments
Hi,
I am trying to use axes() function to set current axes(created in Designer GUI) for plotting.
For example, I prefer to write something like this.
function ButtonPushed(app, event)
axes(app.UIAxe1);
plot(line1);hold on;
plot(line2);hold on;
plot(line3);hold on;
plot(line4);hold on;
plot(line5);hold on;
end
Instead of this with augment ax in every function calls.
function ButtonPushed(app, event)
ax = app.UIAxe1;
plot(ax,line1);hold(ax,'on');
plot(ax,line2);hold(ax,'on');
plot(ax,line3);hold(ax,'on');
plot(ax,line4);hold(ax,'on');
plot(ax,line5);hold(ax,'on');
end
But the axes() function doesn't work as what I expect. The figure is still shown in new popup window.
0 Comments
Accepted Answer
Voss
on 4 Sep 2024
Edited: Voss
on 4 Sep 2024
To plot like that in a uifigure, make the uifigure's HandleVisibility 'on' (it's 'off' by default), e.g.:
app.UIFigure.HandleVisibility = 'on';
where app.UIFigure is your uifigure. You can do that once before plotting, say in the app's startupFcn, and that's sufficient.
"axes(cax) sets the CurrentAxes property of the parent figure to be cax. If the HandleVisibilty property of the parent figure is set to "on", then cax also becomes the current axes."
0 Comments
More Answers (1)
Vinay
on 4 Sep 2024
Hii ZB,
To plot multiple lines on the same figure in MATLAB App Designer, you can specify the axes for each plot and use the `hold on` command to overlay multiple plots. After plotting, you can use the `hold off` command. I tested this approach on my system, and it works. I have attached the code for your reference.
function ButtonPushed(app, event)
ax = app.UIAxes;
hold(ax, 'on');
plot(ax,[1 2 3],[1 2 3]);
plot(ax,[1 2 3],[2 4 6]);
plot(ax,[1 2 3],[3 6 9]);
plot(ax,[1 2 3],[4 8 12]);
plot(ax,[1 2 3],[5 10 15]);
hold(ax, 'off');
end
You can refer to the following documentation for “UIAxes”:
2 Comments
Vinay
on 4 Sep 2024
Hii ZB,
The workaround is to create a helper function which handles the plotting logic and ensures that each plot command targets the UIAxes.I have attached the helper function code to plot the graphs without specifying the axes in the 'plot' function.
% Button pushed function: Button
function ButtonPushed(app, event)
app.plotToUIAxes([1 2 3], [1 2 3]);
app.plotToUIAxes([1 2 3], [2 4 6]);
app.plotToUIAxes([1 2 3], [3 6 9]);
app.plotToUIAxes([1 2 3], [4 8 12]);
app.plotToUIAxes([1 2 3], [5 10 15]);
end
% Helper function to plot data to UIAxes
function plotToUIAxes(app, xData, yData)
% Plot data on the specified UIAxes
plot(app.UIAxes, xData, yData);
hold(app.UIAxes, 'on'); % Ensure hold is on for multiple plots
end
end
See Also
Categories
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!