《又到毕业季》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. 实例
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)elsetask = 0;endendfunction Doit2(~,~)if get(Bt2,'Value')set(Bt1,'Value',0)set(Bt3,'Value',0)task = 2;str = '请选择要修改的圆';set(Text,'String',str)elsetask = 0;endendfunction Doit3(~,~)if get(Bt3,'Value')set(Bt1,'Value',0)set(Bt2,'Value',0)task = 3;str = '请选择要删除的圆';set(Text,'String',str)elsetask = 0;endend%% 鼠标组function ButtonDown(~,~)cp = get(gca,'currentpoint');switch taskcase 1p1 = [cp(1,1),cp(1,2)];tmp = plot(p1(1),p1(2),'r.','Parent',Axes);state = 1;case 2if circleNum>0p1 = [cp(1,1),cp(1,2)];circleIdx = ChooseCircle(circleInfo,p1);set(circleList{circleIdx},'LineStyle','--')state = 1;endcase 3if circleNum>0p1 = [cp(1,1),cp(1,2)];circleIdx = ChooseCircle(circleInfo,p1);state = 1;endendendfunction ButtonUp(~,~)if statecp = get(gca,'currentpoint');switch taskcase 1delete(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 2set(circleList{circleIdx},'LineStyle','-')case 3delete(circleList{circleIdx})circleList(circleIdx) = [];circleInfo(circleIdx,:) = [];circleNum = circleNum-1;endstate = 0;endendfunction ButtonMotion(~,~)if task==2 && statecp = get(gca,'currentpoint');p2 = [cp(1,1),cp(1,2)];dp = p2-p1;if norm(dp)>0.01circleInfo(circleIdx,1:2) = circleInfo(circleIdx,1:2)+dp;[cx,cy] = Updata(basicCircle,circleInfo(circleIdx,:));circleList{circleIdx}.XData = cx;circleList{circleIdx}.YData = cy;p1 = p2;endendendfunction 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;endend%% 子函数% 计算距离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];endend
二、键盘交互
持续更新中
1. 基本知识
set(Fig,'WindowKeyPressFcn',@KeyDown);
set(Fig,'WindowKeyReleaseFcn',@KeyUp);
2. 实例
clear; close all; clcGUI10();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 onplot([-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 1if isvalid(Axes)xalim = get(Axes,'xlim');yalim = get(Axes,'ylim');elsebreakendif state% 速度检测tmp = v;v = v+a*dt;if (tmp(1)*v(1))<=0v(1) = 0;end% 落地碰撞检测p = p+v*dt;if p(2)<rp(2) = r;v(2) = 0;endif norm(v)==0state = 0;endH.XData = cir(:,1)+p(1);H.YData = cir(:,2)+p(2);enddrawnowendfunction KeyDown(~,~)pt = get(gcf,'CurrentCharacter');if strcmpi(pt,'a') % 左v(1) = -dv(1);a(1) = -da(1);state = 1;endif strcmpi(pt,'d') % 右v(1) = dv(1);a(1) = da(1);state = 1;endif strcmpi(pt,'w') % 上v(2) = dv(2);a(2) = da(2);state = 1;endendfunction KeyUp(~,~)pt = get(gcf,'CurrentCharacter');if strcmpi(pt,'a') % 左if t(1,1)==0t(1,1) = now;elset(1,2) = now;if (t(1,2)-t(1,1))*86400<0.5p(1) = p(1)-dv(1)*0.5;t(1,1) = 0;elset(1,1) = t(1,2);endendendif strcmpi(pt,'d') % 右if t(2,1)==0t(2,1) = now;elset(2,2) = now;if (t(2,2)-t(2,1))*86400<0.5p(1) = p(1)+dv(1)*0.5;t(2,1) = 0;elset(2,1) = t(2,2);endendendendfunction out = DrawCircle(r)t = 0:pi/16:2*pi;cx = r*cos(t');cy = r*sin(t');out = [cx,cy];endend
其他
此文为《又到毕业季》MATLAB GUI系列视频的代码。
持续更新中—
如果觉得《《又到毕业季》MATLAB GUI 鼠标键盘交互》对你有帮助,请点赞、收藏,并留下你的观点哦!