MATLAB: Question about axes using GUI in MATLAB

axesgui

I have a GUI with one object: axes and a pushbutton. When pressing the pushbutton, I would like to display on the axes several images. I tried to use subplots but it doesn’t seem to cope: the images go all over the GUI window and not just where the axes are.
Here is the code for my pushbutton:
axes(handles.ManyImage);
subplot(2,2,1);
RGB1 = imread('Image1.TIF');
imagesc(RGB1);
axis off
subplot(2,2,2);
RGB2 = imread('Image2.TIF');
imagesc(RGB2);
axis off
subplot(2,2,3);
RGB3 = imread('Image3.TIF');
imagesc(RGB3);
axis off
I attached the illustrations of my problem.
Thank you for the help!

Best Answer

Well... depending on what you're attempting to do here is a sample pushbutton that you'll have to play around with the spacing.
to test this out create a blank gui and put only a push button and axes with their default tags. Size the axes1 appropriately and then paste this callback in. So for this method I use the the GUIDE's axes1 to figure out the position and the area for the subplots (which for a first pass couldn't think of a way to scale them properly for a generic function such that any MxN subplot layout would work).
Along with this method i believe the GUIDE axes1 is still preserved such that if you do need to do just 1 image and not the subplots then you can delete the handles.SubPlot axes and re-enable axes1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Sampimg{1} = rand(100);
Sampimg{2} = repmat(1:100,100,1);
Sampimg{3} = repmat([1:50 50:-1:1],100,1);
Sampimg{4} = repmat([1:50 50:-1:1]',100,1);
Sampimg{5} = rand(100);
Sampimg{6} = repmat(1:100,100,1);
Sampimg{7} = repmat([1:50 50:-1:1],100,1);
Sampimg{8} = repmat([1:50 50:-1:1]',100,1);
subplotMxN = [2 4];
Units = 'pixels';
set(handles.axes1,'units',Units );
Position = get(handles.axes1,'position');
ULPosition = [Position(1) Position(2)+Position(4) Position(3:4)];
set(handles.axes1,'Visible','off');
plotind = 1;
Vspacing = .1*Position(4);
Hspacing = .1*Position(3);
height = .6*Position(4)/subplotMxN(1);
width = .6*Position(3)/subplotMxN(2);
for Mind = 1:subplotMxN(1)
subVspacing = (Mind-1)*Vspacing;
subposRow = [ULPosition(1:2) 0 0] + [0 -subVspacing-Mind*height width height];
for Nind = 0:subplotMxN(2)-1
subHspacing = (Nind)*Hspacing;
if ~isfield(handles,'SubPlot')
handles.SubPlot(plotind) = axes;
elseif isfield(handles,'SubPlot') & length(handles.SubPlot)<prod(subplotMxN)
handles.SubPlot(plotind) = axes;
end
subpos = subposRow + [subHspacing+Nind*width 0 0 0];
set(handles.SubPlot(plotind),'units',Units)
set(handles.SubPlot(plotind),'Position',subpos)
imagesc(Sampimg{plotind});
plotind = plotind+1;
end
end
disp('debug')
guidata(hObject, handles);