我是靠谱客的博主 自信路灯,这篇文章主要介绍MATLAB使用鼠标交互案例记录,现在分享给大家,希望可以做个参考。

1、前记:

(1)鼠标交互生成三次样条曲线

(2)鼠标交互调整点的位置

2、代码:

(1)首先看一个指定点,并将点按交互序列链接起来的例子

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function draw_lines % Click the left mouse button to define a point % Drag the mouse to draw a line to the next point and % left click again % Right click the mouse to stop drawing % figure('WindowButtonDownFcn',@wbdcb) ah = axes('DrawMode','fast'); clc axis ([1 10 1 10]) function wbdcb(src,evnt) if strcmp(get(src,'SelectionType'),'normal') [x,y,str] = disp_point(ah); hl = line('XData',x,'YData',y,'Marker','.'); text(x,y,str,'VerticalAlignment','bottom');drawnow set(src,'WindowButtonMotionFcn',@wbmcb) elseif strcmp(get(src,'SelectionType'),'alt') set(src,'WindowButtonMotionFcn','') [x,y,str] = disp_point(ah); text(x,y,str,'VerticalAlignment','bottom');drawnow end function wbmcb(src,evnt) [xn,yn,str] = disp_point(ah); xdat = [x,xn]; ydat = [y,yn]; set(hl,'XData',xdat,'YData',ydat); end end function [x,y,str] = disp_point(ah) cp = get(ah,'CurrentPoint'); x = cp(1,1);y = cp(1,2); str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')']; end end

效果:在figure中左键点击,留下该点位置,继续点击,生成的点序列进行线段连接,如此下去直到右键点击生成点和线段结束。(相当于指定点后生成的路径)

(2)在上一个基础上,生成点并生成点的三次样条曲线

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function draw_lines1_splinecurve % Click the left mouse button to define a point % Drag the mouse to draw a line to the next point and % left click again % Right click the mouse to stop drawing clc clear figure('WindowButtonDownFcn',@wbdcb); k=1; ah = axes('DrawMode','fast'); clc grid on; axis ([-20 20 -20 20]);%open the file to save the coordinates into fileID = fopen('coord.txt','w'); function wbdcb(src,evnt) if strcmp(get(src,'SelectionType'),'normal') [x,y,str] = disp_point(ah); %write the coordinates into the file A=[x,y]; fprintf(fileID,'%6.2f %6.2fn',A); hl = line('XData',x,'YData',y,'Marker','.'); text(x,y,str,'VerticalAlignment','bottom');drawnow; set(src,'WindowButtonMotionFcn',@wbmcb); elseif strcmp(get(src,'SelectionType'),'alt') set(src,'WindowButtonMotionFcn',''); [x,y,str] = disp_point(ah); %write the coordinates into the file A=[x,y]; fprintf(fileID,'%6.2f %6.2fn',A); text(x,y,str,'VerticalAlignment','bottom');drawnow; fclose(fileID); end A1=importdata('coord.txt','%f'); h = findobj('type','line','tag','ax1'); delete(h); if size(A1,1)>2 xx=zeros(size(A1,1),1); yy=zeros(size(A1,1),1); for i=1:size(A1,1) xy=str2num(A1{i, 1}); xx(i)=xy(1); yy(i)=xy(2); end hold on fnplt(cscvn([xx';yy'])) end function wbmcb(src,evnt) [xn,yn,str] = disp_point(ah); xdat = [x,xn]; ydat = [y,yn]; set(hl,'XData',xdat,'YData',ydat); end end function [x,y,str] = disp_point(ah) % P1 = impoint(gca,[]); cp = get(ah,'CurrentPoint'); x = cp(1,1);y = cp(1,2); str = ['(',num2str(x,'%0.3g'),', ',num2str(y,'%0.3g'),')']; end end

效果: 各点之间生成光滑三次样条曲线

(3)生成点再次修改点位

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
% Plot a line and points figure plot(1:10,1:10,'-o','buttondownfcn',{@Mouse_Callback,'down'}); % Callback function for each point function Mouse_Callback(hObj,~,action) persistent curobj xdata ydata ind pos = get(gca,'CurrentPoint'); switch action case 'down' curobj = hObj; xdata = get(hObj,'xdata'); ydata = get(hObj,'ydata'); [~,ind] = min(sum((xdata-pos(1)).^2+(ydata-pos(3)).^2,1)); set(gcf,... 'WindowButtonMotionFcn', {@Mouse_Callback,'move'},... 'WindowButtonUpFcn', {@Mouse_Callback,'up'}); case 'move' % vertical move ydata(ind) = pos(3); xdata(ind) = pos(2); set(curobj,'xdata',xdata) set(curobj,'ydata',ydata) case 'up' set(gcf,... 'WindowButtonMotionFcn', '',... 'WindowButtonUpFcn', ''); end end

 

最后

以上就是自信路灯最近收集整理的关于MATLAB使用鼠标交互案例记录的全部内容,更多相关MATLAB使用鼠标交互案例记录内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(99)

评论列表共有 0 条评论

立即
投稿
返回
顶部