我是靠谱客的博主 自信路灯,最近开发中收集的这篇文章主要介绍MATLAB使用鼠标交互案例记录,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、前记:

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

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

2、代码:

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

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)在上一个基础上,生成点并生成点的三次样条曲线

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)生成点再次修改点位

% 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使用鼠标交互案例记录所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部