我是靠谱客的博主 坦率胡萝卜,这篇文章主要介绍《又到毕业季》MATLAB GUI 鼠标键盘交互视频教学地址一、鼠标交互二、键盘交互其他,现在分享给大家,希望可以做个参考。

《又到毕业季》MATLAB GUI 鼠标键盘交互

  • 视频教学地址
  • 一、鼠标交互
    • 1. 基本知识
    • 2. 实例
  • 二、键盘交互
    • 1. 基本知识
    • 2. 实例
  • 其他

眼看六月就要毕业答辩了,你的论文是不是还差一个GUI没有做呢,赶紧学起来。

by 今天不飞了


视频教学地址

《又到毕业季》MATLAB GUI(九)鼠标交互
《又到毕业季》MATLAB GUI(十)键盘交互


一、鼠标交互


1. 基本知识

set(Fig,'WindowButtonDownFcn',@ButtonDown);
set(Fig,'WindowButtonUpFcn',@ButtonUp);
set(Fig,'WindowButtonMotionFcn',@ButtonMotion);
set(Fig,'WindowScrollWheelFcn',@ScrollWheel);


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function GUI9() %% 搭框架 Fig = figure('Position',[600,200,1000,750],'menu','none',... 'Color','white','NumberTitle','off','Name','GUI9'); % 回调 set(Fig,'WindowButtonDownFcn',@ButtonDown); set(Fig,'WindowButtonUpFcn',@ButtonUp); set(Fig,'WindowButtonMotionFcn',@ButtonMotion); set(Fig,'WindowScrollWheelFcn',@ScrollWheel); % 面板 Pnl1 = uipanel(Fig,'Position',[0.05,0.05,0.7,0.9]); Pnl2 = uipanel(Fig,'Position',[0.75,0.05,0.2,0.9]); % 绘图窗 Axes = axes(Pnl1,'Position',[0.1,0.1,0.8,0.8]); axis([-1,1,-1,1]),grid on,hold on % 文本框 str = ''; Text = uicontrol(Pnl2,'style','text',... 'String',str,'Fontsize',16,... 'Units','normalized','Position',[0.1,0.1,0.8,0.5]); % 按钮组 Bt1 = uicontrol(Pnl2,'style','togglebutton',... 'String','创建新圆形','Fontsize',16,'BackgroundColor',[0.6,1,0.6],... 'Unit','normalized','Position',[0,0.9,1,0.1],... 'Callback',@Doit1); Bt2 = uicontrol(Pnl2,'style','togglebutton',... 'String','修改已有圆形','Fontsize',16,'BackgroundColor',[0.6,0.6,1],... 'Unit','normalized','Position',[0,0.8,1,0.1],... 'Callback',@Doit2); Bt3 = uicontrol(Pnl2,'style','togglebutton',... 'String','清除已有圆形','Fontsize',16,'BackgroundColor',[1,0.6,0.6],... 'Unit','normalized','Position',[0,0.7,1,0.1],... 'Callback',@Doit3); %% 成员 % 执行什么任务 task = 0; state = 0; tmp = [];% 存临时句柄 % 记录鼠标位置 p1 = []; p2 = []; % 圆信息 circleInfo = []; % 存圆心半径 circleList = {}; % 存句柄 circleNum = 0; % 数量 circleIdx = 0; % 当前操作对象 basicCircle = DrawCircle(); % 基本结构 %% 按钮组 function Doit1(~,~) if get(Bt1,'Value') set(Bt2,'Value',0) set(Bt3,'Value',0) task = 1; str = '请确定圆心位置'; set(Text,'String',str) else task = 0; end end function Doit2(~,~) if get(Bt2,'Value') set(Bt1,'Value',0) set(Bt3,'Value',0) task = 2; str = '请选择要修改的圆'; set(Text,'String',str) else task = 0; end end function Doit3(~,~) if get(Bt3,'Value') set(Bt1,'Value',0) set(Bt2,'Value',0) task = 3; str = '请选择要删除的圆'; set(Text,'String',str) else task = 0; end end %% 鼠标组 function ButtonDown(~,~) cp = get(gca,'currentpoint'); switch task case 1 p1 = [cp(1,1),cp(1,2)]; tmp = plot(p1(1),p1(2),'r.','Parent',Axes); state = 1; case 2 if circleNum>0 p1 = [cp(1,1),cp(1,2)]; circleIdx = ChooseCircle(circleInfo,p1); set(circleList{circleIdx},'LineStyle','--') state = 1; end case 3 if circleNum>0 p1 = [cp(1,1),cp(1,2)]; circleIdx = ChooseCircle(circleInfo,p1); state = 1; end end end function ButtonUp(~,~) if state cp = get(gca,'currentpoint'); switch task case 1 delete(tmp) p2 = [cp(1,1),cp(1,2)]; R = norm(p1-p2); [cx,cy] = Updata(basicCircle,[p1,R]); h = plot(cx,cy,'LineWidth',5,'Parent',Axes); circleNum = circleNum+1; circleIdx = circleNum; circleInfo(circleNum,:) = [p1,R]; circleList{circleNum} = h; case 2 set(circleList{circleIdx},'LineStyle','-') case 3 delete(circleList{circleIdx}) circleList(circleIdx) = []; circleInfo(circleIdx,:) = []; circleNum = circleNum-1; end state = 0; end end function ButtonMotion(~,~) if task==2 && state cp = get(gca,'currentpoint'); p2 = [cp(1,1),cp(1,2)]; dp = p2-p1; if norm(dp)>0.01 circleInfo(circleIdx,1:2) = circleInfo(circleIdx,1:2)+dp; [cx,cy] = Updata(basicCircle,circleInfo(circleIdx,:)); circleList{circleIdx}.XData = cx; circleList{circleIdx}.YData = cy; p1 = p2; end end end function ScrollWheel(~,event) if task==2 cp = get(gca,'currentpoint'); p2 = [cp(1,1),cp(1,2)]; circleIdx = ChooseCircle(circleInfo,p2); value = event.VerticalScrollCount; % 关键句 circleInfo(circleIdx,3) = max(circleInfo(circleIdx,3)+value*0.01,0.01); [cx,cy] = Updata(basicCircle,circleInfo(circleIdx,:)); circleList{circleIdx}.XData = cx; circleList{circleIdx}.YData = cy; end end %% 子函数 % 计算距离 function idx = ChooseCircle(circleInfo,C) dis = sum((circleInfo(:,1:2)-C).^2,2); [~,idx] = min(dis); end % 更新圆 function [cx,cy] = Updata(basicCircle,w) cx = w(3)*basicCircle(:,1)+w(1); cy = w(3)*basicCircle(:,2)+w(2); end % 生成圆形 function out = DrawCircle() t = 0:pi/32:2*pi; cx = cos(t'); cy = sin(t'); out = [cx,cy]; end end

二、键盘交互

持续更新中


1. 基本知识

set(Fig,'WindowKeyPressFcn',@KeyDown);
set(Fig,'WindowKeyReleaseFcn',@KeyUp);


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
clear; close all; clc GUI10(); function GUI10() %% 搭框架 Fig = figure('Position',[600,500,1000,500],'menu','none',... 'Color','white','NumberTitle','off','Name','GUI10'); set(Fig,'WindowKeyPressFcn',@KeyDown); set(Fig,'WindowKeyReleaseFcn',@KeyUp); % 绘图窗 Axes = axes(Fig,'Position',[0.1,0.1,0.8,0.8]); axis(10*[-1,1,0,1]),grid on,hold on plot([-20,20],[0,0],'k-','LineWidth',3,'Parent',Axes); % 初始化 state = 0; r = 1; cir = DrawCircle(r); p = [0,r]; % 当前位置 v = [0,0]; % 当前速度 dv = [5,10]; % 速度改变值 a = [0,0]; % 当前加速度 da = [-5,-20]; % 加速度改变值 H = plot(cir(:,1)+p(1),cir(:,2)+p(2),'r-','LineWidth',3,'Parent',Axes); drawnow %% 执行 dt = 0.01; t = zeros(2,2); while 1 if isvalid(Axes) xalim = get(Axes,'xlim'); yalim = get(Axes,'ylim'); else break end if state % 速度检测 tmp = v; v = v+a*dt; if (tmp(1)*v(1))<=0 v(1) = 0; end % 落地碰撞检测 p = p+v*dt; if p(2)<r p(2) = r; v(2) = 0; end if norm(v)==0 state = 0; end H.XData = cir(:,1)+p(1); H.YData = cir(:,2)+p(2); end drawnow end function KeyDown(~,~) pt = get(gcf,'CurrentCharacter'); if strcmpi(pt,'a') %v(1) = -dv(1); a(1) = -da(1); state = 1; end if strcmpi(pt,'d') %v(1) = dv(1); a(1) = da(1); state = 1; end if strcmpi(pt,'w') %v(2) = dv(2); a(2) = da(2); state = 1; end end function KeyUp(~,~) pt = get(gcf,'CurrentCharacter'); if strcmpi(pt,'a') %if t(1,1)==0 t(1,1) = now; else t(1,2) = now; if (t(1,2)-t(1,1))*86400<0.5 p(1) = p(1)-dv(1)*0.5; t(1,1) = 0; else t(1,1) = t(1,2); end end end if strcmpi(pt,'d') %if t(2,1)==0 t(2,1) = now; else t(2,2) = now; if (t(2,2)-t(2,1))*86400<0.5 p(1) = p(1)+dv(1)*0.5; t(2,1) = 0; else t(2,1) = t(2,2); end end end end function out = DrawCircle(r) t = 0:pi/16:2*pi; cx = r*cos(t'); cy = r*sin(t'); out = [cx,cy]; end end

其他

此文为《又到毕业季》MATLAB GUI系列视频的代码。
持续更新中—

最后

以上就是坦率胡萝卜最近收集整理的关于《又到毕业季》MATLAB GUI 鼠标键盘交互视频教学地址一、鼠标交互二、键盘交互其他的全部内容,更多相关《又到毕业季》MATLAB内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部