Copy a figure then add more data

Hi,
I created a figure with some data. I would like to copy that figure a couple of times, and add different data to each of the copies. I followed the advice in http://www.mathworks.com/matlabcentral/newsreader/view_thread/172272 to copy the figure, but adding data to the figure doesn't seem to work as expected. When I run the following code, I get a 'Error using ==> isappdata' 'Invalid object handle'. It seems to be an error with the legend function, but I'm not sure how to fix it. Thanks.
x=[1 2 3 4 5];
M1=[1 2 3 4 5];
S1=[2 3 2 1 1];
M2=[2 2 2 2 2];
S2=[1 1 1 1 1];
M3=[5 4 3 2 1];
S3=[2 3 3 3 3];
M4=[5 1 2 6 3];
S4=[1 1 1 1 1];
figure
eIMFig=gcf;
errorbar(x,M1,S1)
hold all
errorbar(x,M2,S2)
legendCell={'mydat1','mydat2'};
legend(legendCell)
figure
h2=gcf;
copyobj(get(eIMFig,'children'),h2);
figure(h2)
hold all
errorbar(x,M3,S3)
errorbar(x,M4,S4)
legendCellNew=legendCell;
legendCellNew{end+1}='mydat3';
legendCellNew{end+1}='mydat4';
legend(legendCellNew)

Respuestas (2)

Matt Fig
Matt Fig el 14 de Ag. de 2012
Editada: Matt Fig el 14 de Ag. de 2012
I am thinking this is a bug. The problem relates to the way legend works after COPYOBJ. It seems that MATLAB does not recognize previous axes children. When I run this, I get a warning:
Warning: Ignoring extra legend entries.
> In legend at 292
In copy_fig_pro at 52
.
.
x=[1 2 3 4 5];
M1=[1 2 3 4 5];
S1=[2 3 2 1 1];
M2=[2 2 2 2 2];
S2=[1 1 1 1 1];
M3=[5 4 3 2 1];
S3=[2 3 3 3 3];
M4=[5 1 2 6 3];
S4=[1 1 1 1 1];
eIMFig=figure;
errorbar(x,M1,S1)
hold all
errorbar(x,M2,S2)
legendCell={'mydat1','mydat2'};
legend(legendCell)
% Now the copying....
h2 = figure;
C = copyobj(get(eIMFig,'children'),h2); % Copies legend too!
figure(h2)
axes(C(2)) % Set plotting axes to current, not legend.
hold all
errorbar(x,M3,S3)
errorbar(x,M4,S4)
legendCellNew=legendCell;
legendCellNew{end+1}='mydat3';
legendCellNew{end+1}='mydat4';
delete(findall(gcf,'tag','legend')); % Delete the copied legend.
drawnow % Update figure
legend(legendCellNew) % Add a new legend, and we get the warning.
.
.
.
Rather than deleting, I tried to set the string of the copied legend, but only got various errors, depending on how I did it.

2 comentarios

Matt Fig
Matt Fig el 14 de Ag. de 2012
Editada: Matt Fig el 14 de Ag. de 2012
I found that replacing the last line of the above with this created the correct legend:
legend(get(C(2),'children'),legendCellNew)
Matt Fig
Matt Fig el 14 de Ag. de 2012
This behavior seems to be related to this bug report.

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 14 de Ag. de 2012

0 votos

The wrong axes ends up as the current axes for some reason (that is, I debugged far enough to determine that the axes was not the one might expect.)
I recommend using the parenting techniques I described at http://www.mathworks.co.uk/matlabcentral/answers/22208-show-figure

Preguntada:

el 14 de Ag. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by