MATLAB: Do I get an error after repetitive usage of the program using the Data Acquisition Toolbox

acquisitiondaqData Acquisition Toolboxgetdatalooprepetitive

Why do I get an error after repetitive usage of my program using the Data Acquisition Toolbox?
I have created a GUI for signal sampling using the Data Acquisition Toolbox and my DAQ card. After repetitive usage of the program the message below appears in the MATLAB command window and the program ceases to acquire new data.
Error event occurred at 11:22:09 for the object: nidaq1-AI.
Timeout in GETDATA.
Error in ErrorAction: 'daqaction'.
Error using ==> daqmexcb
Timeout waiting for device to stop. Stop has been forced.
??? Error using ==> analoginput/getdata
Timeout in GETDATA.
Error in ==> C:\matlabR12\work\SignaltoNoise\DAQcard.m
On line 82 ==> data = getdata(ai);
??? Error while evaluating uicontrol Callback.
How do I get rid of this problem so as to obtain uninterrupted program functionality?

Best Answer

This bug has been fixed in Release 2007b (R2007b). For previous product releases, read below for any possible workarounds:
The problem is that multiple analoginput objects are created, and each analoginput object requires resources not only in the DAQ engine, but also in the Windows COM subsystem. Using DELETE(ai) in a loop will fix the problem. An even better solution is to create a single analoginput object and only perform the triggering and actual acquisition in a loop as the following code illustrates:
ai = analoginput('nidaq', 'Dev1');
addchannel(ai, 0);
set(ai, 'TriggerType', 'manual');
start(ai);
for ii = 1:num_iterations
trigger(ai)
wait(ai, 10)
data = getdata(ai);
my_processing_routine(data);
end
delete(ai)
clear ai