我有一个带2个按钮的简单指南。按钮#1绘制正弦图,按钮#2为图形添加“可拖动”垂直线。 Idea基于以下链接1
我正在使用guidata在GUIDE
中的子功能和回调之间传输数据。但其中一个共享变量(mousedown)不会在回调之间传输。导致子功能之间数据传输丢失的原因是什么?
以下是带有子功能的回调:
% ######### Callbacl 1: Graph SIN function
function pushbutton_plot_Callback(hObject, eventdata, handles)
x = -pi:0.01:pi;
y = sin(x);
fig_h = figure('units','normalized','outerposition',[0.2 0.2 .5 .5],'WindowButtonMotionFcn', @MouseMove, 'WindowButtonUpFcn', @MouseUp );
axis_h = axes('parent',fig_h,'position',[0.1 0.1 .8 .8]);
line('parent',axis_h,'xdata',x,'ydata',y);
handles.axis_h = axis_h;
guidata(hObject,handles);
%### MouseUp subfunctions in callback 1
function MouseUp(h,event) %#ok<*INUSD>
mousedown = false;
end
%### MouseUp subfunctions in callback 1
function MouseMove(h,event)
if isfield(handles,mousedown)
mousedown = handles.mousedown;
else
mousedown = false;
end
if mousedown
cp = get ( axis_h, 'CurrentPoint' );
vertline_h = handles.vertline_h;
set ( vertline_h, 'XData', [cp(1,1) cp(1,1)] );
end
end
end
%######### Callbacl 2: Graph movables vertical line
function pushbutton_vertline_Callback(h, eventdata,handles)
axis_h = handles.axis_h;
vertline_h = line('parent',axis_h,'xdata',[1 1],'ydata',[-1 1], 'ButtonDownFcn', @mouseDown );
handles.vertline_h = vertline_h;
guidata(h,handles)
%### MouseMove subfunction in callback 2
function mouseDown(hObject,event)
mousedown = true;
handles.mousedown = mousedown;
guidata(hObject,handles)
end
end