MATLAB: Want an explanation to what this code does? “ax2h = get(ax2(1),’Children’);”

merging figures; get;

I am writing a code to merge two plots and what I found on matlab community is the following code:
fh1 = open('Plot1.fig');
fh2 = open('Plot2.fig');
ax1 = get(fh1, 'Children');
ax2 = get(fh2, 'Children');
ax2h = get(ax2(1),'Children');
copyobj(ax2h, ax1(1));
What I could not figure out was what does ax2(1) refer to?

Best Answer

That code could use some some improvement.
The code opens two .fig files. In each .fig file, it get()'s the children of the figure, and assumes that the first child is an axes. Then it get()'s the children of the axes of the second figure and copies them into the first axes. The children of an axes are the things that are drawn in the axes. The code is copying them into [what is assumed to be] an existing axes that already has tick marks and xlim and so on.
In particular, when you get() the Children of a figure, you normally only get the children whose HandleVisibility property is set to 'on'. Most graphics complex graphics objects such as polar graphs or legends are created with HandleVisibility set to either 'off' or 'callback', and so will be missed by the code that is shown in the Question above.