matlab鼠标拖动图形代码
function test()
clear
clc
figure();
plot((1:0.5:10)',sin(1:0.5:10)'*(1:10),...
'LineWidth',3,'ButtonDownFcn', @bdfcn);
drawnow
haxes=gca;hfigure=gcf;
haxes.XLimMode='manual';
haxes.YLimMode='manual';
hsel=[]; % 选择的线
op=[0,0];% 初始点
function bdfcn(hline,edata)
% edata.Button=1 左键, 3 右键
op=edata.IntersectionPoint(1:2);
hsel=hline;
hsel.Selected='on';
[email protected];
[email protected];
end
function wbmfcn(h,e)
cp = haxes.CurrentPoint; % 鼠标位置
hsel.XData=hsel.XData+cp(1,1)-op(1);
hsel.YData=hsel.YData+cp(1,2)-op(2);
op=cp(1,1:2);
end
function wbufcn(h,e)
hfigure.WindowButtonMotionFcn='';
hfigure.WindowButtonUpFcn='';
hsel.Selected='off';
end
clear
clc
figure();
plot((1:0.5:10)',sin(1:0.5:10)'*(1:10),...
'LineWidth',3,'ButtonDownFcn', @bdfcn);
drawnow
haxes=gca;hfigure=gcf;
haxes.XLimMode='manual';
haxes.YLimMode='manual';
hsel=[]; % 选择的线
op=[0,0];% 初始点
function bdfcn(hline,edata)
% edata.Button=1 左键, 3 右键
op=edata.IntersectionPoint(1:2);
hsel=hline;
hsel.Selected='on';
[email protected];
[email protected];
end
function wbmfcn(h,e)
cp = haxes.CurrentPoint; % 鼠标位置
hsel.XData=hsel.XData+cp(1,1)-op(1);
hsel.YData=hsel.YData+cp(1,2)-op(2);
op=cp(1,1:2);
end
function wbufcn(h,e)
hfigure.WindowButtonMotionFcn='';
hfigure.WindowButtonUpFcn='';
hsel.Selected='off';
end
end