MATLAB DAQ Toolbox: How do I store the output from a callback function during a running session?

5 views (last 30 days)
I have a callback function in my DAQ session that displays a message when a certain input-voltage exceeds a specified limit. What I would like to get is the data of the time whenever the voltage exceeded that limit (s.t. in the end I can calculate the rate of these voltage exceedings).
This is the DAQ script:
daq.getDevices;
s = daq.createSession('ni');
s.Rate = 100;
s.DurationInSeconds = 5;
s.addAnalogInputChannel('Dev1','ai3','Voltage');
%s
lh = addlistener(s,'DataAvailable',@(src,event,voltagelimit)voltageexceed(src,event,0.03));
[data,time]=s.startForeground;
and this is the callback function:
function voltageexceed( src, event,voltagelimit)
if any(event.Data > voltagelimit)
fprintf('Detected voltage exceeds %f V \n',voltagelimit)
end
Is there a way to get this time data as an output of the callback function whenever the listener runs the callback function? I.e. outputting event.TimeStamps(1) every time the callback function runs and then saving this value in a vector in the DAQ script itself? Saving event.TimeStamps into a mat file in the callback function is not satisfactory because the mat file gets overwritten every time.
Edit: I was able to do it with loading and saving the mat file every time the callback is triggered. Like this:
function voltageexceed( src, event,voltagelimit)
if any(event.Data > voltagelimit)
fprintf('Event listener: Detected voltage exceeds %f V \n',voltagelimit)
newtime = event.TimeStamps(1);
timeold = load('timedatafile.mat','time');
timeold = timeold.time;
time=[timeold; newtime];
save('timedatafile','time');
end
Is there a more elegant way? I also tried it with .bin files as described for data logging for the session based interface here:
Yet I am not able to save decimals with any precision attribute for the fwrite function. So how can I save and read decimals with a .bin file? I also don't quite see why the data gets appended to the .bin file because when the logData function is called the file access type is still 'w'. Wouldn't it have to be 'a' s.t. this
data = [evt.TimeStamps, evt.Data]' ;
fwrite(fid,data,'double');
saves the new data to the end of the .bin file?

Accepted Answer

himmellaeufer
himmellaeufer on 6 Aug 2015
I think I solved it myself to a satisfactory degree. Here the codes if another one who is new to DAQ Toolbox has a similar problem:
daq.getDevices;
s = daq.createSession('ni');
s.Rate = 100;
s.IsContinuous = true;
s.addAnalogInputChannel('Dev1','ai3','Voltage');
s
fileID1 = fopen('timeofexceedfile.mat','w');
fileID2 = fopen('timeanddatafile.mat','w');
voltagelistener = addlistener(s,'DataAvailable',@(src,event,voltagelimit,fileID) voltageexceed(src,event,0.03,fileID1));
plotlistener = addlistener(s,'DataAvailable',@(src,event) plot(event.TimeStamps,event.Data));
dataloglistener = addlistener(s,'DataAvailable',@(src,event,fileID) logData(src,event,fileID2));
s.startBackground;
command = input('','s'); %just to stop the daq
if strcmp(command,'evaluate daq')
s.stop;
fclose(fileID1);
fclose(fileID2);
fileID1 = fopen('timeofexceedfile.mat','r');
fileID2 = fopen('timeanddatafile.mat','r');
timeofexceed = fread(fileID1,[1 inf],'double');
timeanddata = fread(fileID2,[2 inf],'double');
fclose(fileID1);
fclose(fileID2);
end
The data log function
function logData(src, event, fileID)
timeanddata = [event.TimeStamps event.Data]';
fwrite(fileID,timeanddata,'double');
end
And the voltage exceed event callback
function voltageexceed( src, event,voltagelimit,fileID)
if any(event.Data > voltagelimit)
fprintf('Event listener: Detected voltage exceeds %1.2f V \n time of exceed: %1.1f s \n',voltagelimit,event.TimeStamps(1));
fwrite(fileID,event.TimeStamps(1),'double');
end
  2 Comments
Walter Roberson
Walter Roberson on 6 Aug 2015
All of the listeners you add to s should have the same number of parameters in the @(variables) list. In particular they should have exactly two parameters, src and event. You are getting away with it because you never happen to refer to those extra parameters.
It is not a good idea to name an arbitrary binary file as .mat as you are going to get those confused with MATLAB save files.
Nicolas Echeverry
Nicolas Echeverry on 3 Sep 2018
Is there any way to access an element on that binary file like is done in a matrix? I need the previous data of the signal so I can apply convolution on it in real time...

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!