MATLAB: How to display 10 images on a MATLAB GUI and then drag and drop each of them individually in order to arrange them in a particular order

drag and drop imagesmatlab gui

Hi,
I need to display 10 different images simultaneously on a MATLAB GUI using 10 different axes graphical object. Then each of these images needs to be drag and drop in the GUI in order to make a particular arrangement. I have used drag_n_drop.m function found on the internet with some modifications. I can drag and drop a single axes with out images by using this. Once I add an images, it is producing 'Reference to non-existent field 'axes1'' error message. I am attaching the drag_n_drop function and the GUI opening function, which I have for this purpose.
.m
function drag_n_drop(fig_handle, eventdata)
%Get position of Mouse location in figure
%handles.output = hObject;
handles = guihandles(fig_handle);
pos = get(fig_handle, 'currentpoint'); % get mouse location on figure
x = pos(1); y = pos(2); % assign locations to x and y
% get position information of the uicontrol
bounds = get(handles.axes1,'position');
lx = bounds(1); ly = bounds(2);
lw = bounds(3); lh = bounds(4);
% test to see if mouse is within the uicontrol and make a hand sign
if x >= lx && x <= (lx + lw) && y >= ly && y <= (ly + lh)
setfigptr('hand', handles.figure1);
set(handles.axes1, 'ButtonDownFcn', @grab);
else
setfigptr('arrow', handles.figure1);
end
function grab(target_handle, eventData)
% to be attached to ButtonDownFcn of the uicontrol
% change the shape of the pointer to closed hand
setfigptr('closedhand', fig_handle);
% computing x, y difference between pointer and uicontrol
% so that the movement can be in sync
mouse_pos = get(fig_handle, 'currentpoint');
target_pos = get(handles.axes1,'position');
dx = mouse_pos(1) - target_pos(1);
dy = mouse_pos(2) - target_pos(2);
% attach drag and release to WindowButtonMotionFcn and
% WindowButtonUpFcn
set(fig_handle,'WindowButtonMotionFcn', @drag);
set(fig_handle,'WindowButtonUpFcn',@release);
function drag(fig_handle, eventData)
% to be attached to WindowButtonMotionFcn
% get mouse x,y location on figure
mouse_pos = get(fig_handle, 'currentpoint');
x = mouse_pos(1); y = mouse_pos(2);
% get uicontrol width and height
target_pos = get(handles.axes1,'position');
tw = target_pos(3); th = target_pos(4);
% update uicontrol position based on the computed x,y locations
target_pos = [x-dx, y-dy, tw, th];
set(handles.axes1,'position', target_pos);
end % end of drag function
function release(fig_handle, eventData)
% to be attached to WindowButtonUpFcn
% update pointer back to hand shape
setfigptr('hand', fig_handle);
mouse_pos = get(fig_handle, 'currentpoint');
x = mouse_pos(1); y = mouse_pos(2);
set(fig_handle, 'WindowButtonMotionFcn', @drag_and_drop);
set(fig_handle, 'WindowButtonUpFcn', '');
end % end of release function
end % end of grab function
end % end of drag_and_drop function
GUI Opening Function
% --- Executes just before rank is made visible.
function rank_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to rank (see VARARGIN)
% Choose default command line output for rank
axes(handles.axes1);
img=imread('camo_part.jpg');
imshow(img);
handles.output = hObject;
set(hObject,'WindowButtonMotionFcn',@drag_n_drop);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes rank wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
I can drag and drop blank axes using this function. But when I add an image into the axes object, this code produces an error "Reference to non-existent field 'axes1' ". Please suggest if I can avoid this? Or please suggest a better method to achieve similar functionality?

Best Answer

sajeesh - try using guidata rather than guihandles when trying to get the handles structure
function drag_n_drop(fig_handle, eventdata)
handles = guidata(fig_handle);
% etc.
Not all handles can be returned using guihandles (see documentation for why).