MATLAB: MATLAB DAQ Toolbox: How to store the output from a callback function during a running session

callback functionData Acquisition ToolboxMATLAB

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?

Best Answer

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