MATLAB数据光标模式

时间:2015-11-08 01:00:19

标签: matlab datatip

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

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 个答案:

答案 0 :(得分: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