Reputation: 131
I am trying to implement a listener in my MATLAB DAQ program, which notifies, if enough data are available in my acquired data matrix (and then saves a backup etc).
I know that there is something similar existing in the Data Acquisition Toolbox with the event 'DataAvailable'
and NotifyWhenDataAvailableExceeds
(frequency, when the event is fired). Unfortunately my device is not supported by the toolbox and I set up a DAQ program by myself by using a timer and querying the instrument values with fprintf
and fscanf
(Instrument Control Toolbox).
For a first approach to implement this, I read the documentation (i.a. Events and Listeners Syntax) and tried to implement an example, where an empty matrix is expanded by a for
loop and a listener is notifying, when there are e.g. 100 data points available. However, I failed completely, because I couldn't figure out, how to implement, how the listener "notifies" this. Is it even possible to create "own" events or can MATLAB only listen to already existing events given in the documentation?
I am not sure if I got the concept right and would be glad if someone would give me a quick overview, if and how it is possible, what I am trying to do.
Below you find a Code example. I am totally aware, that it can't work like this yet, but I hope it makes clear what I am trying to do:
%% Class definition
classdef DAQClass < handle
properties
DataMatrix %Does this make sense to put the matrix as property?
%Maybe better as event data?
end
events
EnoughData
end
methods %Does it make sense to put the condition when to notify, in a method??
%how can I access it for my listener
function IfEnoughDataAvailable(obj,DataMatrix)
if (length(DataMatrix)>100)
notify(obj,'EnoughData');
end
end
end
end
%% Add listener, run code to fill data matrix
DAQObject=DAQClass;
DAQObject.DataMatrix=[];
lh = addlistener(DAQObject,'EnoughData',@reactToEvent);
for k=1:1000
DAQObject.DataMatrix(k)=k;
end
Upvotes: 1
Views: 114
Reputation: 24127
I'd implement it like this, using a property listener rather than a listener (notice that I've put DataMatrix
to be SetObservable
):
classdef DAQClass < handle
properties (SetObservable)
DataMatrix = []
end
properties (Access = private)
DataMatrixListener
end
methods
function obj = DAQClass
obj.DataMatrixListener =...
addlistener(obj, 'DataMatrix', 'PostSet', @obj.reactToEvent);
end
function reactToEvent(obj, ~, ~)
if length(obj.DataMatrix)>100
disp('Enough data available') % Do actual thing here
end
end
end
end
Then you can run:
>> d = DAQClass
d =
DAQClass with properties:
DataMatrix: []
>> for i = 95:105
disp(i)
d.DataMatrix = 1:i;
end
95
96
97
98
99
100
101
Enough data available
102
Enough data available
103
Enough data available
104
Enough data available
105
Enough data available
Note that the reactToEvent
method does the length-checking itself, and reactToEvent
is called whenever DataMatrix
changes. So you might want to put some logic in there so that it's only called once, when DataMatrix
increases beyond 100, rather than every time it's beyond 100 (e.g. it could process the content of DataMatrix
and then reset DataMatrix
to []
, treating it like a buffer.
Upvotes: 1