Help with dragging lines in GUI using draggable.m

15 views (last 30 days)
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!!
  3 Comments
HpW
HpW on 31 May 2018
Thanks for the reply. Ive tried multiple things. Sometimes I get an error, and sometimes nothing happens but the function doesnt work.
function new_line_xcoord = update_drag_line_values(line)
handles = guidata(hObject); % load handles variables
drag_line_xdata = round(get(line,'XData'));
new_line_xcoord = drag_line_xdata(1)
Gives 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.
If I try to add handles in this way:
function new_line_xcoord = update_drag_line_values(line,hObject, eventdata, handles)
handles = guidata(hObject); % load handles variables
drag_line_xdata = round(get(line,'XData'));
new_line_xcoord = drag_line_xdata(1)
I get error:
Not enough input arguments.
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.
I dont know how to get handles to be active in this function....thanks for the help!
HpW
HpW on 31 May 2018
to clarify, im trying to access the handles variables (handles.variable1) etc from within
function new_line_xcoord = update_drag_line_values(line)

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 31 May 2018
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.
  4 Comments
HpW
HpW on 2 Oct 2018
Sorry for the long delay in my reply --- i missed that there was a reply. The code is:
function new_line_xcoord = update_drag_line_values(line)
drag_line_xdata = round(get(line,'XData'));
new_line_xcoord = drag_line_xdata(1);
hGui = findobj('Tag','GUI1');
if ~isempty(hGui)
handles = guidata(hGui);
end
old_coord = str2num(get(handles.edit_old_beat_textbox,'String'));
old_coord (1) = new_line_xcoord;
set(handles.edit_old_beat_textbox,'String',num2str(old_beat));
update_coords_Callback(hObject, eventdata, handles)
It crashes with the
Undefined function or variable 'hObject'.
error when I add the final line for calling update_coords_Callback. Any help would be appreciated. thx!
Geoff Hayes
Geoff Hayes on 10 Oct 2018
since hObject has not been defined as an input parameter to your function (and because you haven't created a variable of the same name) when you try to pass hObject to your update_coords_Callback then you get the error. Should the handle be a part of the handles structure and so you can get it from there?

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!