MATLAB: How to create a new figure window which contains the subplot when I click on the subplot on the main figure window in MATLAB 7.7 R2008b

MATLAB

I have a figure having 255 subplots. I want to click on one of the subplots such that it opens the same subplot in a new figure window.

Best Answer

The following example demonstrates how to achieve this functionality:
function example_plot
% This is an example of using the ButtonDownFcn to create a new figure which
% contains the subplot that the user selected on the main figure window
figure
for i=1:12
subplot(12,1,i)
plot (sin(1:100)*10^(i-1))
set(gca,'XTick',[],'YTick',[],'ButtonDownFcn',@createnew_fig)
end
% Reset the bottom subplot to have xticks
set(gca,'XTickMode', 'auto');
end
function createnew_fig(cb,evendata)
%cb is the handle of the axes that was clicked
%click on the whitespace within and axes and not on the line object
%copy the axes object to the new figure
hh = copyobj(cb,figure);
%for the new figure assign the ButtonDownFcn to empty
set(hh,'ButtonDownFcn',[]);
%resize the axis to fill the figure
set(hh, 'Position', get(0, 'DefaultAxesPosition'));
end