Copy graphics from figure to UIAxes

조회 수: 40 (최근 30일)
Jason
Jason 2023년 4월 7일
편집: Jason 2023년 4월 7일
Hello, I create a plot on a UIAxes with many plots and text objects. I wanted to be able to save them and later on add additional data so I have managed tconvert all the graphics objects on my UIAxes to a figure using the following:
ax=app.UIAxes;
xlab=ax.XLabel; ylab=ax.YLabel;
yl=ax.YLim;
h = findobj(ax,'Type','line');
h1 = findobj(ax,'Type','text');
h2=findobj(ax);
fig=figure;
set(gcf,'color',[0.25,0.25,0.25]);
ax = axes;
new_handle = copyobj(h,ax); new_handle = copyobj(h1,ax);
grid(ax,'on');
ax.YLim=yl;
ax.XLabel=xlab; ax.YLabel=ylab;
I am then able to save this fig.
However, after re-opening this saved fig and trying to get the graphics objects from the figure to the UIAxes is where I am struggling.
I have tried:
ax=app.UIAxes;
%Get directory where data is
try
[file,folder]=uigetfile({'*.fig'},'Open Fig file',app.startfolder);
app.startfolder=folder;
catch
[file,folder]=uigettfile({'*.fig'},'Open Fig file','C:\');
end
openpath=fullfile(folder,file);
h=openfig(openpath);
ax=axes(h);
drawnow;
%f=gcf
%copyobj(h.Children, app.UIAxes)
%newax = copyobj(h,ax);
newax = copyobj(ax,app.UIAxes); %from, to
But get the error:
Error using copyobj
Axes cannot be a child of UIAxes.

채택된 답변

Jason
Jason 2023년 4월 7일
편집: Jason 2023년 4월 7일
Done it, had to get the children of the axes on the figure!
h=openfig(openpath);
Ax=h.Children
g=Ax.Children
%ax=axes(h);
n=numel(g)
hold(ax,'on');
for i=1:n
T=g(i).Type
newax = copyobj(g(i),app.UIAxes);
end
drawnow;
But one last question, can I get the graphics objects from the saved .fig wihtout actually opening it? (and then having to close it)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by