MATLAB: How to make the figure axes pop out in a new figure when I click on them in MATLAB 7.6 (R2008a)

MATLABpop-uppopoutundockzoom

I have a GUI or a supblot with many axes. When I click on an axes I would like the axes to pop out in a new figure window.

Best Answer

You can set the ButtonDownFcn property of each axes object to point to a function that will be called when a mouse-click occurs on the axes. This callback function will receive the handle to the calling axes as input and can use the COPYOBJ function to copy the axes and its children to a new figure. The ButtonDownFcn property of the axes can be set during plotting or, as in this example, after a figure with many axes has been created:
axh = findobj(gcf,'Type','axes');
for i = 1:length(axh)
set(axh(i),'ButtonDownFcn',@popout);
end
The callback function "popout" will need to create a new figure, copy the axes object to the new figure and then modify the Position property of the copied axis to fill the figure. The ButtonDownFcn of the new axis will also need to be changed to empty to prevent clicks from spawning new figures. For example, one implementation of "popout" is as follows:
function popout(src,eventdata)
figure;
copyobj(src,gcf);
set(gca,'Position',[.13 .11 .77 .815],'ButtonDownFcn',[]);
Also, note that the callback function will be called only when a mouse-click occurs on the axes and not on any lines or patches that may be on top of the axes. If you desire this behavior you can set the ButtonDownFcn property of all these objects.
For more information on the ButtonDownFcn property of the axes and the COPYOBJ function, please consult the documentation by executing the following at the MATLAB prompt:
web([docroot '/techdoc/ref/axes_props.html#ButtonDownFcn'])
doc copyobj