MATLAB: Help with dragging lines in GUI using draggable.m

guihandlesMATLABmatlab gui

Hi. Im trying to use the handy draggable function https://www.mathworks.com/matlabcentral/fileexchange/4179-draggable for a GUI im working on. I want to drag a line and have the x coordinate update in a textbox after the user releases the mouse. Im having some issues implementing it due to some issue with handles that i cant figure out.
This is how I added the draggable line:
axes(handles.axis1)
plot(y)
hold on
q_line = line([x1 x1],[y2 y1], 'color', 'k','linewidth',0.5, 'linestyle', '--');
draggable(qon_line,'h',[x1-50 x2+50],'endfcn',@update_drag_line_values);
then i have a separate function:
function new_line_xcoord = update_drag_line_values(line)
drag_line_xdata = round(get(line,'XData'));
new_line_xcoord = drag_line_xdata(1)
but when I try to do anything involving handles I get either this error:
Undefined variable "handles" or class "handles.variable1".
Error in GUI_name>update_drag_line_values (line 4381)
handles.variable1
Error in draggable>deactivate_movefcn (line 311)
feval(user_endfcn,h); % added by SMB, modified by FB

Error while evaluating Figure WindowButtonUpFcn.
or this error:
Undefined function or variable 'hObject'.
Error in GUI_name>update_drag_line_values (line 4378)
handles = guidata(hObject); % load handles variables
Error in draggable>deactivate_movefcn (line 311)
feval(user_endfcn,h); % added by SMB, modified by FB
Error while evaluating Figure WindowButtonUpFcn.
Ive tried to pass handles into the endfcn in various ways including:
axes(handles.axis1)
plot(y)
hold on
q_line = line([x1 x1],[y2 y1], 'color', 'k','linewidth',0.5, 'linestyle', '--');
draggable(qon_line,'h',[x1-50 x2+50],'endfcn',{@update_drag_line_values,handles});
but nothing seems to work….any help would be appreciated. thanks!!

Best Answer

HpW - ok, so you have written a function called update_drag_line_values that you pass in to the draggable function (from the FEX). The draggable function then calls your function.
The Not enough input arguments makes sense because you have added several input parameters to update_drag_line_values but draggable doesn't supply these additional parameters/arguments when calling your function.
The Undefined function or variable 'hObject' makes sense too since the code is trying to access a variable that hasn't been defined.
You can probably do one of two things: pass the GUI figure handle into draggable (you will need to update this code) so that it passes this handle into your function. So your function signature and first couple of lines would become
function new_line_xcoord = update_drag_line_values(line, hFigure)
handles = guidata(hFigure);
% etc.

And you will need to figure out where in draggable the figure handle gets passed along to your function.
Alternatively, given that you have a handles structure, I'm going to assume that you are using GUIDE to develop your GUI. If you follow https://www.mathworks.com/matlabcentral/answers/146215-pass-data-between-gui-s, what we need to do is set the HandleVisibility property to on, and the Tag property to Gui1 (or whatever tag/name you want for your GUI). Then, in your update_drag_line_values function we would do
function new_line_xcoord = update_drag_line_values(line)
hGui = findobj('Tag','Gui1');
if ~isempty(hGui)
handles = guidata(hGui);
% etc.
end
The second approach is simpler and you don't have to worry about updating the draggable function.