Dang Khoa
Dang Khoa

Reputation: 5823

MATLAB - how to use event.eventData, and efficiently

For background information and context for my question, please read this question.

Notice in the updatePlot() method of my DynamicPlotter code, I kind of "reach into" a DynamicDataset property, as follows:

function updatePlot(obj, propNum)
    X = get(obj.LH(propNum), 'XData');
    Y = get(obj.LH(propNum), 'YData');

    X(end+1) = obj.(dynProps{propNum}).newestData(1);
    Y(end+1) = obj.(dynProps{propNum}).newestData(2);

    set(obj.LH(propNum), 'XData', X, 'YData', Y);
end

updatePlot is a listener callback. Rather than "reach in" to go get the newestData, I am wondering if it would be more efficient to have the data "presented" to the callback with event.eventData. But I am unsure of (a) how to even use event.eventData (the example provided in the documentation isn't very clear to me), and (b) if this yields better or worse performance.

So I guess my main question is, what's the best way for updatePlot() to access the newestData as depicted in the method above: "reaching in and retrieving it" or using event.eventData to "send" the data point to the function to plot?

I hope this wasn't terribly ambiguous.

Upvotes: 0

Views: 2652

Answers (1)

anon
anon

Reputation:

You first need to have a class that defines an event (in MyClass.m):

classdef MyClass < handle
  events
    % Event
    MyEvent
  end

  methods
    function obj = MyClass
      % Constructor
    end
  end
end

Then you need to create your own EventData subclass (in MyEventData.m):

classdef MyEventData < event.EventData
  properties (Access = public)
    % Event data
    Data
  end

  methods
    function this = MyEventData(data)
      % Constructor
      this.Data = data;
    end
  end
end

Attach your data to an instance of the event data class (in a script file):

X = 1:10;
Y = 1:10;
data = struct('XData', X, 'YData', Y);
eventData = MyEventData(data);

And fire the event from your obj and have a listener listen to it:

obj = MyClass;
l = addlistener(obj, 'MyEvent', @(evtSrc,evtData)disp(evtData.Data));
notify(obj, 'MyEvent', eventData)

Anytime you call notify(...), the evtData argument in your listener callback will have your data in its Data property:

>> notify(obj, 'MyEvent', eventData)
    XData: [1 2 3 4 5 6 7 8 9 10]
    YData: [1 2 3 4 5 6 7 8 9 10]

Upvotes: 2

Related Questions