How to close a DAQ session with triggers remaining
10 views (last 30 days)
Show older comments
Hi,
I'm using the DAQ toolbox to send a pattern to a motor whenever an external trigger arrives. For now, I can successfully drive the motor upon trigger arrival. However, since I do not know the exact number of triggers during the recording session (dependent on animal), I set the d.NumDigitalTriggersPerRun to 200 to ensure the motor moves every time theres a trigger.
However, this introduce a problem as I can't manually close the DAQ session, I've tried following without success:
- type stop(d) in command window
- ctrl-c
- Force re-run by clicking run above (matlab will always crash)
My code can be simplified as follows, thanks in advance!
clear; close all
d = daq('ni')
d.NumDigitalTriggersPerRun = 200;
% lines to addinput
% lines to addoutput
addtrigger(d,"Digital","StartTrigger","External",'Galvo/PFI0'); % add trigger
% lines to generate motor pattern
% run session
stop(d); % stop session if required
flush(d);
preload(d,pattern);
start(d);
2 Comments
Walter Roberson
on 15 Aug 2023
You "clear" so we know that there are no variables remaining in memory -- that in particular there is no d before the assignment you show.
You then have a structure assignment, and pass the structure to addtrigger(). But addtrigger() does not accept struct: it expects DataAcquisition objects.
Accepted Answer
Ninad
on 28 Aug 2023
Edited: Ninad
on 31 Aug 2023
The script may not immediately stop when using ‘stop(d)’ or ‘Ctrl+C’ due to the asynchronous nature of DAQ operations. In an asynchronous session, data acquisition tasks run independently of the main program flow, allowing concurrent execution. MATLAB's DAQ operations default to asynchronous mode, enabling multitasking during acquisition. This behaviour is advantageous as it allows monitoring progress, processing acquired data, and performing calculations simultaneously, enhancing efficiency.
You can use a onCleanup method to detect the ‘Ctrl+C’ command.
cleanupObj = onCleanup(cleanupFun) creates an object that, when destroyed, executes the function cleanupFun. MATLAB® implicitly clears all local variables at the termination of a function, whether by normal completion, or a forced exit, such as an error, or Ctrl+C.
PF following link:
3 Comments
Ninad
on 31 Aug 2023
Thanks for pointing out Walter. OnCleanup is a function provided for the similar purpose as needed by Shun.
Walter Roberson
on 31 Aug 2023
Except that Shun is working with a script, not a function. Shun would have to deliberately wrap code into a function to take advantage of onCleanup . Which would probably be acceptable to do.
More Answers (0)
See Also
Categories
Find more on Data Acquisition Toolbox Supported Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!