How come my Daq Session isnt continous?

19 views (last 30 days)
michael
michael on 18 Oct 2025 at 19:52
Commented: michael on 20 Oct 2025 at 17:05
Hello,
I am trying to have my Daq read continously,plot the data and also look for a trigger and have a certain number of pre trigger samples, for some reason even though its set to continous,it wont run unless I put a pause at the bottom,and then it'll only run for the alotted time of the pause.So 100 seconds....why.... I would just like it to plot indefinitely and later use a Stop UI button to stop session..
function DaqAq101825
% Create a Data Acquisition session
daqSession = daq.createSession('mcc'); % Replace 'ni' with your DAQ vendor if different
% Add an analog input channel (e.g., channel 'ai0')
addAnalogInputChannel(daqSession, 'Board0', 'ai0', 'Voltage'); % Replace 'Dev1' and 'ai0' with your device and channel
% Set session properties
daqSession.Rate = 1000; % Sampling rate in Hz
% daqSession.DurationInSeconds = 20; % Duration of acquisition
% Pre-trigger configuration
daqSession.IsContinuous = true; % Enable continuous acquisition
daqSession.NotifyWhenDataAvailableExceeds = 500; % Trigger callback every 500 samples
% Threshold and pre-trigger setup
threshold = 0.5; % Define your threshold value
preTriggerSamples = 1000; % Number of pre-trigger samples to store
% Initialize variables for plotting
dataBuffer = []; % Buffer to store pre-trigger and post-trigger data
triggered = false; % Flag to indicate if the trigger has occurred
figure; % Create a figure for real-time plotting
% Define the callback function
lh = addlistener(daqSession, 'DataAvailable', @(src, event) processData(src, event));
% Start the acquisition
startBackground(daqSession);
% Callback function to process data
function processData(src, event)
% Append new data to the buffer
dataBuffer = [dataBuffer; event.Data];
% Check if the trigger condition is met
if ~triggered && any(event.Data > threshold)
triggered = true;
disp('Threshold reached! Triggering...');
end
% Keep only the last 'preTriggerSamples' in the buffer
if size(dataBuffer, 1) > preTriggerSamples
dataBuffer = dataBuffer(end-preTriggerSamples+1:end, :);
end
% Plot the data
plot(dataBuffer);
title('Real-Time Data Acquisition');
xlabel('Samples');
ylabel('Voltage');
drawnow;
end
pause(100); % <------whyyyy is this the only way for it to run
stop(daqSession);
delete(lh); % Remove the listener
end

Answers (2)

Walter Roberson
Walter Roberson on 18 Oct 2025 at 20:28
You are telling the daq session to stop. That stopping happens near immediately after the stop() call is encountered.
Remember that your processData is not itself looping, so it is going to be invoked at most a small number of times before the stop() is encounted.
If you want a pause button you will need to program one. You might potentially be able to get away with as little as a questdlg() call; I suspect that the listener will continue in the background while the question dialog is in the foreground.

Umar
Umar on 19 Oct 2025 at 14:23

@Michael — the behavior you’re seeing is actually expected with how MATLAB handles background DAQ sessions. When you call `startBackground`, the acquisition and callback (`processData`) do run continuously in the background, but your main function keeps executing right away. Since the very next line after `startBackground` is `stop(daqSession)`, the session ends almost immediately — that’s why it only seems to run when you add a `pause()`.

The `pause(100)` doesn’t really make the DAQ “run for 100 seconds” — it just blocks MATLAB from reaching the `stop()` call for that long, so your background listener keeps firing during that pause. Once the pause finishes, MATLAB executes `stop()`, and everything shuts down.

@Walter Roberson is absolutely right that your `processData` callback isn’t looping by itself and that you’re stopping the session prematurely. The DAQ continues to generate events while MATLAB’s main thread is alive, but once your function ends, it’s all cleaned up.

If you want to run continuously and stop manually, you’ll need something that keeps MATLAB’s event loop active — for example, a small UI window with a “Stop” button or even a `questdlg()` prompt would work. That way, the acquisition continues until you tell it to stop, instead of depending on an arbitrary pause duration.

So in short:

  • The DAQ is continuous; it’s your script that’s ending too soon.
  • The `pause()` just delays the script from finishing.
  • The clean solution is to replace the pause with a user-controlled stop (like a button or dialog).

Hope this helps.

Categories

Find more on Data Acquisition Toolbox Supported Hardware in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!