Clear Filters
Clear Filters

Plotting data in realtime using SensorDAQ (NI DAQ)

20 views (last 30 days)
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.

Accepted Answer

Eddy H
Eddy H on 15 Jun 2017
Edited: Eddy H on 15 Jun 2017
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.

More Answers (4)

Eddy H
Eddy H on 8 Jun 2017
UPDATE: I have run this code with a different sensor and a different NI card (NI USB-6343) and it worked perfectly.
Any suggestions why the original card might not work, and what settings I might be able to change to make it work?

Eddy H
Eddy H on 9 Jun 2017
UPDATE: An update from this morning, so I have narrowed it down further:
Dev1 is the SensorDAQ, Dev2 is the X series (NI USB 6343).
This code plots both datasets at the end of the script:
s = daq.createSession('ni');
ch1 = s.addAnalogInputChannel('Dev1','_sensor0_5V','Voltage');
ch2 = s.addAnalogInputChannel('Dev2','ai0','Voltage');
s.Rate = 100;
s.DurationInSeconds = 10;
s.NotifyWhenDataAvailableExceeds = 100;
lh = addlistener(s,'DataAvailable', @(src,event) plot(event.TimeStamps, event.Data));
startBackground(s);
disp('Go')
s.wait();
delete(lh)
Whereas, if I comment-out Dev1, this code plots the data from Dev2 in realtime.
s = daq.createSession('ni');
% ch1 = s.addAnalogInputChannel('Dev1','_sensor0_5V','Voltage');
ch2 = s.addAnalogInputChannel('Dev2','ai0','Voltage');
s.Rate = 100;
s.DurationInSeconds = 10;
s.NotifyWhenDataAvailableExceeds = 100;
lh = addlistener(s,'DataAvailable', @(src,event) plot(event.TimeStamps, event.Data));
startBackground(s);
disp('Go')
s.wait();
delete(lh)
Any idea what in the SensorDAQ might be causing this?

Grace Steward
Grace Steward on 3 Aug 2021
As of August 3 2021, this problem with the SensorDAQ still exisits. I just got a email from Matlab support saying that they are looking at fixing this issue in future version of Matlab; however, with the SensorDAQ being discontinued earlier this year, support seems unlikely.
Vernier support was much more helpful. They suggested creating short duration continuous sampling session and running repeatedly through a loop to achieve a high sampling rate with close to real time feedback. However, there will be some gaps in the collection using this method.
I'm hoping to post my code shortly as an example.

Grace Steward
Grace Steward on 26 Aug 2021
After being in contact with Matlab for a few weeks, I found that increasing the sampling rate from 1kHz to 24 kHz solves this issue.

Community Treasure Hunt

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

Start Hunting!