2015-11-08 98 views
0

我有一个简单的类来绘制基本的x和y数据。在类中,我有一种方法可以启用数据光标模式,自定义文本,并收集和保存点。我想改变方法的行为,以便我一次只能收集两点。现在,即使在关闭数据光标模式并将其重新打开以使用它时,它也会存储每个点。这是我对我的类代码:MATLAB数据光标模式

classdef CursorPoint 


    properties 
     Xdata 
     Ydata 
    end 

    methods 
     function me = CursorPoint(varargin) 

      x_data = 0:.01:2*pi; 
      y_data = cos(x_data); 
      f= figure; 
      plot(x_data,y_data); 
      me.DCM(f); 

     end 

     function DCM(me,fig) 
      dcm_obj = datacursormode(fig); 

      set(dcm_obj,'UpdateFcn',@myupdatefcn) 
      set(dcm_obj,'enable','on') 
      myPoints=[]; 

      function txt = myupdatefcn(empt,event_obj) 
       % Customizes text of data tips 

       pos = get(event_obj,'Position'); 
       myPoints(end + 1,:) = pos; 
       txt = {['Time: ',num2str(pos(1))],... 
        ['Amplitude: ',num2str(pos(2))]}; 

      end 

     end 

    end 
end 

回答

1

难道你myPoints变量更改为两个变量称为myPointCurrentmyPointPrevious。当您调用myupdatefcn方法时,您会将myPointCurrent的内容移动到myPointPrevious,然后将当前位置存储在myPointCurrent中。

新的功能(有一些错误检查)看起来像:

function txt = myupdatefcn(empt,event_obj) 
    % Customizes text of data tips 
    myPointPrevious=myPointCurrent; 

    pos = get(event_obj,'Position'); 
    myPointCurrent=pos; 

    txt = {['Time: ',num2str(pos(1))],... 
     ['Amplitude: ',num2str(pos(2))]}; 
end 
+1

作品意! – DeeTee