Get data from callback without global variable?
이전 댓글 표시
Hi,
I want to continuously acquire data in the background using the Data Acquisition Toolbox as described here. I have a callback function that is called when data is available. This callback can be used to log/process the data.
What is the most elegant/efficient way to make the acquired data available to other functions? Basically, at any point while the acquisition is running in the background, I want to be able to execute commands from the command line such that these commands have access to the data that has just been acquired.
One solution I can think of is to have a global variable in the 'DataAvailable' callback, which I can then access from other functions, but I'm not sure if that's the best solution.
Thanks!
댓글 수: 1
Matthias
2014년 5월 20일
이동: Walter Roberson
2026년 4월 17일 11:07
답변 (1개)
Adeline
2026년 4월 17일 7:59
Using global variables to access data from a DAQ callback is not recommended. This is consistent with MATLAB best practices and with other data‑acquisition environments (for example, LabVIEW), where global state makes code harder to maintain and debug.
In most cases, you do not need to export data from the callback to the base workspace. The callback already receives the acquired data, and it can be passed to other functions for processing. This is the same pattern used in the example from documentation, where data is processed or plotted directly from the callback.
The example below shows how data acquired in the callback can be passed to another function and processed without using global variables:
dq = daq("ni");
addinput(dq,"Dev1","ai0","Voltage");
dq.ScansAvailableFcnCount = 100; % Required number of scans for callback execution
dq.ScansAvailableFcn = @processDataAvailable;
start(dq,"continuous");
pause(5);
stop(dq);
%%%% Callback function: runs when more than 100 scans of data is available
function processDataAvailable(src, ~)
[data,ts] = read(src,src.ScansAvailableFcnCount,OutputFormat="Matrix");
% Pass acquired data to another function for processing
[avgVal,avgTs] = processData(data,ts);
% Example use of processed result
fprintf('Mean value at %s: %f\n', string(avgTs), avgVal);
end
%%%% Processing function: operates on data from the callback
function [avgValue,avgTime] = processData(data,time)
avgValue = mean(data,1); % Compute mean of current data block
avgTime = time(end); % Use last timestamp as reference
end
댓글 수: 1
Walter Roberson
2026년 4월 17일 11:12
Although this analysis is not actually wrong, it overlooks the requirements,
Basically, at any point while the acquisition is running in the background,
I want to be able to execute commands from the command line such that these
commands have access to the data that has just been acquired.
Notice the commands are to be run at the command line -- at irregular intervals. Because of this, it is necessary to accumulate all of the data between command line runs (and potentially all of the data since the start of the session.) There is no automatic calculation being applied to chunks of data as they stream through: the data needs to accumulate in a buffer until the user calls for it to be used.
카테고리
도움말 센터 및 File Exchange에서 Analog Data Acquisition에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!