Help with dragging lines in GUI using draggable.m
Show older comments
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
Geoff Hayes
on 31 May 2018
HpW - please show the code where you are attempting to access handles.variable1. If the handles structure is not passed in as an input parameter (to your function) then you cannot access it...which is what the error messages is telling you as handles or hObject are undefined. You don't necessarily have to pass in handles to your function but you need to do something to get at this object (i.e. define it) before you try to use it in your code.
HpW
on 31 May 2018
HpW
on 31 May 2018
Accepted Answer
More Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!