2015-11-08 85 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