Child swapping

Today I am going to present a technique that I have used extensively to deal with figures. I have never seen it named before or even presented anywhere on the web so I decided to call it “Child Swapping” (edit : Yair from Undocumentedmatlab rightfully pointed out that this technique is often called re-parenting. I like my own name too though so I kept the post title). I believe this technique is very useful when you have to manipulate figure windows, reuse them in a different context or concatenate them together. So if I managed to tickle your curiosity, read on.

This started when a colleague came to me about an interesting problem he was facing. He had recorded many many figures in fig files and wanted to concatenate them in a smart way on a single figure. These figures were all organized in a similar layout and he wanted to pick one plot on each figure and create a new figure made of all the chosen plots. One could have said :

Well, run your calculation again to make this new figure.

But in this particular example, this was annoying as it took many hours of calculation and work to generate these fig files. Another alternative was to use each figure handle and access the underlying data through their XData and YData fields. But this was not the most elegant solution. Then we remembered that figure handles are nicely organized in a tree structure with several layers of parents and childs. And as opposed to our little human families, parents and childs can be swapped at will in Matlab without any legal issues.

Hold on, let me backtrack a little bit and explain …

If you have followed my post on handles, you know that each figure in Matlab has an address which you can use to gather all of its properties. For instance, if you are to do :

x=1:100;
y=x.^2;
figure(1);
plot(x,y);

You will display a figure with a very simple plot on it.

You can then do :

>> h=get(1,’Children’)

h =

279.0443

What’s happening?
‘Children’ is a property of the figure window. It stores all the handles that are children of this figure. In other words, it stores all the graphical objects that are displayed on this particular window. Given that we created a plot on it, then 279.0443 is the handle to this plot. Indeed, if you do :

>> get(h,’Type’)

ans =

axes

You see that this handle is a link to the axes object that stores our plot. Axes is a generic Matlab graphical objects than can contain many types of plot so it has some children too :

>> h2=get(h,’Children’);
get(h2,’Type’)

ans =

line

There lies our plot. It is a line object. Indeed, if you do :

>> yline=get(h2,’YData’);

You get the same y dataset that we sent to plot. Keep in mind that h2 also has a ‘Parent’ property that points at the axes object. Matlab keeps track of its families properly!

Now the trick is that you can change these values. Let’s play with that. Try this :

figure(2);
set(h,'Parent',2);

If you look carefully. Your plot has magically been transferred to the figure 2 and disappeared from the figure 1. Child Swapping just happened without any paperwork!

You can use this technique to transfer anything from one figure to another : axes, buttons, text, images, …

Now, why is this useful?

First you can, as we said, load figure files and rearrange them as you wish but most importantly this offer a means to programmatically resort figures based on user interaction. For instance you could create a figure with checkboxes to pick axes of interest and superpose selected axes on a new figure.

This is great info. I just developed a way to do this independently for one of my projects that required a little more finesse than just get(h,’Children’). I ended up using the findobj(h,…) with specific parameters to grab JUST the objects I wanted, e.g. to grab the axes but not the legend as they are both children of the figure.

A useful thing I found was by setting the tag parameter of the objects that end up on the figure makes them easier to find using findobj(). You’d probably only do this when you knew ahead of time that you would want specific objects though.