Copying all contents of a figure into another figure in Matlab GUIDE by clicking on a figure?

70 views (last 30 days)
I have two sets of axes: (axes1_1, axes1_2) and (axes2_1, axes2_2). When I click on either of the sets, I would like to plot them both on a larger axes set (axes_large1, axes_large2), as shown in the image below. If either axes1_1 or axes1_2 are clicked, the axes1_1 should be traced on axes_large1 and axes1_2 should be traced on axes_large2. Same would be the case when axes2_1 and axes2_2 are clicked.
Here is my code:
function plotMyImages(handles, data, depth, Score, flag)
if flag
h1 = handles.axes1_1;
h2 = handles.axes1_2;
else
h1 = handles.axes2_1;
h2 = handles.axes2_2;
end
cla(h1, 'reset');
im1 = imagesc(h1, 'CData', data, 'YData', depth);
set(h1, 'YDir', 'reverse'); box(h1, 'on');
xlabel(h1, 'Distance');
ylabel(h1, 'Depth') ;
cla(h2, 'reset');
im2 = plot(h2, Score, '*');
xlabel(h2, 'Distance');
appel = @show_in_figure;
set(im1, 'ButtonDownFcn', appel(flag)); % ButtonDownFcn modified to show figure on the larger axes
set(im2, 'ButtonDownFcn', appel(flag)); % ButtonDownFcn modified to show figure on the larger axes
The show_in_figure() is as follows:
function show_in_figure(hObject, event, handles, flag)
hfig = findobj('Type', 'axes', 'Tag', 'axes_large1');
copyobj(hObject, hfig);
xx = get(gca, 'XLim'); yy = get(gca, 'YLim');
set(hfig, 'XLim', xx);
set(hfig, 'YLim', yy);
xlabel(hfig, 'Distance');
ylabel(hfig, 'Depth');
hfig1 = findobj('Type', 'axes', 'Tag', 'axes_large2');
if flag
h1 = handles.axes1_2;
else
h1 = handles.axes2_2;
end
copyobj(h1, hfig1);
xlabel(hfig1, 'Distance');
Is there anything that I'm doing wrong here? I am using MATLAB 2018a.

Accepted Answer

Rik
Rik on 17 Nov 2020
(Caveat: I haven't tried running any of this code)
What are you expecting to happen when you set the ButtonDownFcn? It doesn't look like you're actually setting a function there, but are evaluating the function instead. Why not modify it like this?
appel = @(h,e)show_in_figure(h1,h2);
% ^^^ this capture the default inputs of a callback: the object handle (see doc:gcbo) and the event data
set(im1, 'ButtonDownFcn', appel); % ButtonDownFcn modified to show figure on the larger axes
set(im2, 'ButtonDownFcn', appel); % ButtonDownFcn modified to show figure on the larger axes
function show_in_figure(h1, h2)
handles=guidata(h1);
copyobj(h1,handles.handle_to_axes_large1)%copy image object to second axes
copyobj(h2,handles.handle_to_axes_large2)%copy image object to second axes
%if you want to copy the axis labels etc you can put that code here
end
Do not rely on gca inside GUIs, as you can always determine the handle. You can use gcbo and gcbf if you want, but you should never need findobj.
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
  2 Comments
Shreedhar Savant Todkar
Shreedhar Savant Todkar on 17 Nov 2020
Edited: Shreedhar Savant Todkar on 17 Nov 2020
I actually wanted to draw the images/plots that are clicked onto the larger plots. With the following modification, I was able to get the desired result. I replaced your lines:
copyobj(h1,handles.handle_to_axes_large1) %copy image object to second axes
copyobj(h2,handles.handle_to_axes_large2) %copy image object to second axes
with this:
copyobj(get(h1,'Children'), handle_to_axes_large1);
copyobj(get(h2,'Children'), handle_to_axes_large2);
because I got the following error with your code:
Axes cannot be a child of Axes
Rik
Rik on 17 Nov 2020
Edited: Rik on 17 Nov 2020
Ah yes, I forgot you need to copy the image object, not the axes object. Use this instead:
appel = @(h,e)show_in_figure(im1,im2);
For consistency you can change the names in show_in_figure as well, but you don't need to.
Edit (after seeing your edit): yes, copying all children should work as well, maybe even better, as you don't have to modify other properties, like axis labels etc.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!