MATLAB: Select an image inside a figure

select image mouse figure

Hello everyone I have to select one image within a figure (in which there are 4 pictures) and save the result. Can anyone help me? Thanks a lot F

Best Answer

something like this can be implemented.
function interactiveplot()
% 4 images
hax(1)=subplot(2,2,1);imagesc(rand(20,10)); title('image1')
hax(2)=subplot(2,2,2);imagesc(rand(30,20));title('image2')
hax(3)=subplot(2,2,3);imagesc(rand(40,30));title('image3')
hax(4)=subplot(2,2,4);imagesc(rand(50,40));title('image4')
% create a context menu (right click)
for ind=1:length(hax)
hcmenu(ind) = uicontextmenu;
set(hax(ind),'userdata',ind); %tag which subplot is which
%create the save listing in the context menu, passing it the
%callbackfunction savesub to save with the associated subplot
%handle
item = uimenu(hcmenu(ind),'Label','save subplot','Callback',{@savesubs hax(ind)});
%set what the context menue will be assigned to.
% set(hax(ind),'uicontextmenu',hcmenu(ind)) %not sure if needed
% seems to work without it
set(get(hax(ind),'children'),'uicontextmenu',hcmenu(ind))
end
end
%function that does the saving
function savesubs(h,event,hax)
%figure out where and what you want to call the image
[file folder] = uiputfile('*.jpg');
%create new figure to display. if not all subplots will be saved
hfig = figure;
hax_new = copyobj(hax, hfig);
set(hax_new, 'Position', get(0, 'DefaultAxesPosition'));
%save the copied plot as a jpg.
saveas(hfig,file,'jpg')
%close the new plot
close(hfig);
end