Copy graphics from figure to UIAxes

10 vues (au cours des 30 derniers jours)
Jason
Jason le 7 Avr 2023
Modifié(e) : Jason le 7 Avr 2023
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.

Réponse acceptée

Jason
Jason le 7 Avr 2023
Modifié(e) : Jason le 7 Avr 2023
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)

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by