Set current axes by using axes() is not wokring

9 次查看(过去 30 天)
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.

采纳的回答

Voss
Voss 2024-9-4
编辑:Voss 2024-9-4
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.
Note that this is stated in the axes documentation:
"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."

更多回答(1 个)

Vinay
Vinay 2024-9-4
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 个评论
ZB
ZB 2024-9-4
Hi Vinay,
Thanks for your answer, but I am hoping that ax can be also removed from plot() function. That's why I am using axes() function but it doesn't work as what I learned from document and other topics.
Vinay
Vinay 2024-9-4
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by