Select an image inside a figure

2 views (last 30 days)
Fed
Fed on 25 Feb 2015
Commented: Joseph Cheng on 25 Feb 2015
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

Accepted Answer

Joseph Cheng
Joseph Cheng on 25 Feb 2015
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
  2 Comments
Fed
Fed on 25 Feb 2015
Thanks! that is just amazing, much better compared to what I wanted! But, my originally idea was to select the image with the mouse, simply clicking on one of the four images. I didn't use the subplot function, rather I change the position of each image in order to locate it in different part of the figure. I used the function ginput to get the coordinates of the position of the mouse when clicked. But after that I am lost! I should check if it is inside or outside the axis limits of each image.Bu the output of the ginput are not reasonable. Maybe can you help me? Thank you again!
Joseph Cheng
Joseph Cheng on 25 Feb 2015
From memory i think Ginput coordinates are figure/window referenced so you'll need to know the clickable regions defined and hard code them in for regions. so you'd determine mouse position (x,y) is within region1, or region2, etc. that can be implemented through nested if statements or probably a case statement.
But maybe i'm confusing it with another mouse input function (or insane). a quick query online for determining which one was clicked appears to have been answered here http://www.mathworks.com/matlabcentral/newsreader/view_thread/297774.
My implementation was a "quick" solution while i was waiting on something to process in my day job. Without some more testing i wasn't sure what you'd like to do once you've selected to plot so i went with a right click. The subplots were just a very quick way to generate 4 different axes.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!