1
votes

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

1
Do you want to copy two existing figures or do you just want to have 2 plots (generated by yourself) in the same figure? In the latter case you can just use hold mathworks.ch/ch/help/matlab/ref/hold.html (since you say that you are a beginner, but I don't know how much, I just want to make sure you are not overcomplicating) - lib
I am aware of "hold on". The problem is I generate 30+ plots from multiple comma-seperated datalog files and the script I have is designed to parse each column of a data file before going on to the next file. My end goal is to group certain columns from different files. I could re-write my script, i suppose, but in CSV form I exploit the fact that reading a line at a time is more parallel in nature than doing 1 column of a file, switching files with hold on, then repeating this per column. The code already takes a few hours to run while parsing all columns simultaneously line-by-line. - MrHappyAsthma
I seem to have found a way to make this work. See my answer below for more info -- There may be a more elegant way to do this. But it should work well enough for my needs. - MrHappyAsthma
The solution is quite good, and I don't think there is a more elegant way.Consider also if having those plots open is dragging down your performance. Try to run your script without graphic, and if the time gain is good,consider to save directly save in memory the columns you read (even if they are more data than what you need in each plot) and plot later just what you need. - lib

1 Answers

5
votes

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