Help with dragging lines in GUI using draggable.m
이전 댓글 표시
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
Geoff Hayes
2018년 5월 31일
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
2018년 5월 31일
HpW
2018년 5월 31일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!