MATLAB Data Cursor Mode

550 Views Asked by At

I have a simple class that plots basic x and y data. Within the class I have a method that enables the data cursor mode, customizes the text, and collects and saves the points. I'd like to change the behavior of the method so that I can collect only two points at a time. Right now it stores every point even when I turn off the data cursor mode and turn it back on to use it. Here is my code for my class:

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

There are 1 best solutions below

0
On BEST ANSWER

Could you change the myPoints variable to two variables called myPointCurrent and myPointPrevious. When ever the myupdatefcn method is called you would move the contents of myPointCurrent into myPointPrevious and then store the current position in myPointCurrent.

The new function (with some error checking) would look something like:

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