MATLAB: Plotting data in realtime using SensorDAQ (NI DAQ)

data acquisitionnational instrumentsnirealtimesensordaqstartbackground

Hi everyone,
I am trying to plot data in real-time, as acquired from a NI card (a little different to usual, as it is a SensorDAQ, but should work the same – http://www.vernier.com/products/interfaces/sdaq/). I have followed the instructions as per the MATLAB documentation (https://uk.mathworks.com/help/daq/examples/acquire-continuous-and-background-data-using-ni-devices.html), but it won't plot in real-time. It just waits until the end of the script and then plots the figure.
addpath('SensorDAQ')
s = sdaq.createSession;
sdaq.addSensor(s,1,sdaq.Sensors.HandDynamometer);
s.Rate = 100;
s.DurationInSeconds = 5;
s.NotifyWhenDataAvailableExceeds = 100;
lh = addlistener(s,'DataAvailable', @(src,event) plot(event.TimeStamps, event.Data));
startBackground(s);
s.wait();
delete(lh)
I have also tried alternatives, for example using a nested function with drawnow. But it still doesn't plot anything until the script has finished running. See below:
function test_script()
addpath('SensorDAQ')
s = sdaq.createSession;
sdaq.addSensor(s,1,sdaq.Sensors.HandDynamometer);
s.Rate = 100;
s.DurationInSeconds = 5;
s.NotifyWhenDataAvailableExceeds = 100;
lh = addlistener(s,'DataAvailable', @plotData);
s.startBackground()
wait(s)
delete(lh)
function plotData(src,event)
plot(event.TimeStamps,event.Data)
drawnow
pause(0.1)
end
end
Any ideas? I've tried running this on Matlab 2014a and 2016b.

Best Answer

Hi all,
In case anyone else in the future is battling with this, I got a response from Matlab:
• The issue with startBackground is a known issue with NI SensorDAQ and the Vernier SensorDAQ.
• The developers are working on this and the issue will be resolved in one of the future releases.
• Currently there is no workaround for the issue
i.e. Currently the output cannot be plotted as in real-time.
Unfortunately, the README document that comes with the package suggests you can use startBackground and this 'known' problem isn't mentioned anywhere... I've contacted matlab to ask them to update this, so hopefully this won't continue to be an issue going forward.
Related Question