Automatically Copy Legends from multiple UIAxes into Figure (in order to save that figure)

10 views (last 30 days)
Hi!
I created an app, that consists of multiple Tabs, with multiple sub-Tabs each.
Every one of those sub-Tabs displays a couple of UIAxes. There is one save button on each Sub-Tab, that copies the UIAxes into a new figure and places a couple other objects into that figure (for example annotations).
My Problem is, that with copyobj, the legends do not get copied.
Is there a swift way of getting the legends into the new figure easily, without manually redoing all of them?
Code example of how the elements are created:
% Example of how a UIAxes is created in the app (Only the legend part is interesting i guess):
plot(app.UIAxes_S3_lu,fs,real(H4),fs,imag(H4));
axis(app.UIAxes_S3_lu,[0 30 -10 10]);
legend(app.UIAxes_S3_lu,'Realteil','Imaginärteil','NorthEast');
%
stem(app.UIAxes_S3_ru,fs,abs(H4));
hold(app.UIAxes_S3_ru,'on');
stem(app.UIAxes_S3_ru,fh,abs(H4h),'r');
axis(app.UIAxes_S3_ru,[0 30 0 10]);
legend(app.UIAxes_S3_ru,'Original','höhere Auflösung','NorthEast');%%
hold(app.UIAxes_S3_ru,'off');
%
% How the save Button is generally coded:
newfigure = figure('Position',[0.1*app.ss(3), 0.1*app.ss(4), 0.75*app.ss(3), 0.75*app.ss(4)]);
%
copyobj(app.UIAxes_S3_lu, newfigure);
copyobj(app.UIAxes_S3_ru, newfigure);
annotation (newfigure,'Example')
% etc
savefig(newfigure,'C:\User...');
delete(newfigure);
  3 Comments
Adam Danz
Adam Danz on 5 Feb 2021
copyobj is the way to go as of r2020b but is not supported prior to that (or 2020a, I forget).
Note that copyUIAxes() from the file exchange also copies UIAxes and their legends (and colorbars) to another figure from Matlab r2016a to the current release (currently r2020b).

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!