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
Matthias 2014 年 5 月 20 日
移動済み: Walter Roberson 2026 年 4 月 17 日 11:07
Any ideas? Are globals the way to go here?

サインインしてコメントする。

回答 (1 件)

Adeline
Adeline 2026 年 4 月 17 日 7:59

0 投票

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
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.

サインインしてコメントする。

カテゴリ

質問済み:

2014 年 5 月 20 日

コメント済み:

2026 年 4 月 17 日 11:12

Community Treasure Hunt

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

Start Hunting!

Translated by