Select an image inside a figure
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
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.
More Answers (0)
See Also
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!