From: Mayi DA on
Dear all,
I want to copy a figure to another figure including axes and legend, so I use the following test code:

function test()
close all;
h1 = figure;
ha = subplot(1, 1, 1);
hp1 = plot([rand(10, 1)], 'parent', ha);
leg1_handle = legend(ha, {'data line'});

h2 = figure;
na = copyobj(ha, h2);
leg2_handle = legend(na, 'String', get(leg1_handle, 'String'));
return;
this test.m can work correctly, then you can see two identical figure that contained the same plot and legend. However, when replace 'plot' with 'bar' in above .m code,

function test()
close all;

h1 = figure;
ha = subplot(1, 1, 1);
hp1 = bar([rand(10, 1)], 'parent', ha); % use 'bar' to replace 'plot'
leg1_handle = legend(ha, {'data line'});

h2 = figure;
na = copyobj(ha, h2);
leg2_handle = legend(na, 'String', get(leg1_handle, 'String'));
return;
then there will be a warning message. and legend dose not show on the figure.
Warning: Plot empty.
> In legend at 294
In test at 11


Best Regards
mayi
2010-07-19
From: Jan Simon on
Dear Mayi,

> h1 = figure;
> ha = subplot(1, 1, 1);
> hp1 = bar([rand(10, 1)], 'parent', ha); % use 'bar' to replace 'plot'
> leg1_handle = legend(ha, {'data line'});
>
> h2 = figure;
> na = copyobj(ha, h2);
> leg2_handle = legend(na, 'String', get(leg1_handle, 'String'));
> return;
> then there will be a warning message. and legend dose not show on the figure.
> Warning: Plot empty.
> > In legend at 294

Hard stuff. As far as I can see, the copied BAR object has an Annotation, whose IconDisplayStyle is set to 'off'. Therefore the command LEGEND assumes, that it cannot show a legend for the BAR handle.

My impression: LEGEND is deeply confused due to its enormous level of complexity. Then your question would be a case for TMW's support. But perhaps it is just me, who is confused by the interactive scribe objects.

Sorry for not helping, Jan