MATLAB: How to speed up data acquisition

acquisitiondataData Acquisition Toolboxfastreadreal timeslow

Best Answer

There are 2 solutions that could potentially work to improve the speed of data acquisition. Note these solutions might work, as there could be limitations imposed by both the hardware and the algorithms used with daq.
Solution 1 (introduced R2010b): "startForeground" or "startBackground" function with a callback
Both of these functions utilize a session based interface, which can invoke callback methods at various points in time. For example, the following will continuously read data in the background:
s = day.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod1',0,'Current');
s.NotifyWhenDataAvailableExceeds = 1; % Reads every individual scan
s.IsContinuous = true; % Data is constantly being acquired
s.Rate = 1000;
listener_handle = addlistener(s, 'DataAvailable', @listener); % Once data is available, calls "listener" function
startBackground(s); % Note: This is non-blocking. If you wish to block on this line, use "startForeground"
function listener(src, event)
disp(event.Timestamps)
disp(event.Data)
end
Solution 2 (introduced R2020a): "start" function with "read" function
Both of these functions utilize an interface to communicate directly with a channel. For example, the following will continuously read data:
Note that the "continuous" flag ensures that the "read" function is available for data acquisition every subsequent call. It is good to use the "stop" function after no more data acquisition is needed.
s = daq("ni");
addinput(s, "cDAQ1Mod1", 0, "Current");
s.Rate = 1000;
start(s, "continuous");
while ...
[data, t] = read(s, "OutputFormat", "Matrix")
end
stop(s)