Question

After much searching, I have found many similar questions to this, however I still cannot get copyobj() to simply copy a specific child object/handle.

(Note: I am pretty new to MATLAB graphics, so my terminology/understanding of figures/handles may be wrong. Feel free to address this!)

Basically every other example seems to use copyobj() in conjunction with allchild(). The problem stems from the fact that some children objects overwrite the legend/title/etc as well as the actual line.

For example, take the following code using two dummy plots:

f = figure;
plot(1:10);
title('plot 1');

f2 = figure;
plot(fliplr(1:10));
title('plot 2');

This will produce the following plots:

Plots 1

If I follow the suggestion involving allchild(), my first plot becomes IDENTICAL to my second plot. I want both lines to be graphed. You will also notice that the old "Plot 1" title now shows the collision of both titles being copied over-top of one another:

copyobj(allchild(f2),f);

Plot collision

So after more research I thought I could just extract the "Line" child instead of all the child handles, and add that to my first plot. This causes an error:

copyobj(findobj(f2, 'Type', 'line'), f);

Throws the following error:

??? Error using ==> copyobj
Object line[1] can not be a child of parent
figure[1]

My ultimate goal would be something like this (illustration done in MS Paint. Please disregard the ugliness):

Goal plot

Was it helpful?

Solution

After a bit more reading here: http://www.mathworks.com/help/matlab/learn_matlab/understanding-handle-graphics-objects.html I came across this chart regarding graphics objects:

Chart

It seems using allchild() must somehow get Axes objects, which can then be plotted to a figure. The error being thrown was a result of trying to plot a Line (Plot Objects) directly to a Figure.

If I plot the Line (Plot Object) to an Axes instead, it works perfectly:

f = figure;
plot(1:10);
title('plot 1');

f2 = figure;
plot(fliplr(1:10));
title('plot 2');

copyobj(findobj(f2, 'Type', 'line'), gca(f));

Final plot

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top