Copy graphics from figure to UIAxes

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