Set current axes by using axes() is not wokring

조회 수: 31 (최근 30일)
ZB
ZB 2024년 9월 4일
편집: Voss 2024년 9월 4일
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