Home > database >  How do I copy the legend of the corresponding plot as well as the plot himself into another plot?
How do I copy the legend of the corresponding plot as well as the plot himself into another plot?

Time:07-06

I'm working with the following Code to copy a saved plot into another.

fig1 = openfig('ABC.fig');
fig2 = openfig('DEF.fig', 'invisible');
copyobj(fig2.Children.Children, fig1.Children);

This works fine, as long as I'm not using a legend in fig2, then I get a Error message for using too many input arguments in copyobj. In the documentation I learnt, that copyobj doesn't copy the context menu of legends, so I'm asking for your help in copying the legend as well.

CodePudding user response:

As shown here:

plot(rand(2))
l = legend('show'); % legend
ax = gca; % associated axes

fnew = figure;
copyobj([l,ax],fnew) 
% This is what the error was telling you. 
% You need to pass your axes object 
% along with you legend object as an array of graphic objects

[l,ax]

ans =

1×2 graphics array:

Graphics    Graphics
  • Related