Note that there are some explanatory texts on larger screens.

plurals
  1. POContinuously acquire data in MATLAB using callbacks and persistent variables
    primarykey
    data
    text
    <p>I have just recently started to use MATLAB to acquire data off of a data acquisition board and was in need of a function to acquire data continuously (i.e. until I ctrl^C out of the function). To do this I am using the data acquisition toolbox on a 32-bit windows OS.</p> <p>Based on the documentation in matlab help and a few of the answers on this site, I found that after adding channels to my input handle I should:</p> <ol> <li>set my 'SamplesPerTrigger' to Inf</li> <li>set the 'TimerPeriod' to some value to trigger the 'TimerFcn'</li> <li>set the 'TimerFcn' to some subfunction callback which appends data to a persistent variable</li> </ol> <p>Is this a correct way to do this?</p> <p>My code is as follows:</p> <pre><code> function acquire_arena_test(samprate,daq_device ,device_ID ,channels, saveroot) setup.SampleRate = samprate; setup.DAQdevice = {daq_device, device_ID}; setup.AIChannels = channels; setup.SaveRoot = {saveroot}; ai = analoginput(setup.DAQdevice{1},setup.DAQdevice{2}); addchannel(ai,[setup.AIChannels]); set(ai,'SamplesPerTrigger',Inf); set(ai,'TimerPeriod',0.5); set(ai,'TimerFcn',{@AcquireData,ai}); start(ai); while(strcmpi(get(ai,'Running'),'On')) pause(1) end stop(ai); time = datestr(now,30); save([saveroot time], 'data'); delete(ai); clear ai; function AcquireData(hObject, ~) persistent totalData; data = getdata(hObject); if isempty(totalData) totalData =data; else totalData = [totalData; data]; end </code></pre> <p>The initial analog input is definitely working properly. I have tried <em>many</em> permutations of giving the AcquireData callback to 'TimerFcn'. The error I receive is </p> <p>`??? Error using ==> acquire_arena_test>AcquireData Too many input arguments.</p> <p>Warning: The TimerFcn callback is being disabled. To enable the callback, set the TimerFcn property. `</p> <p>Thanks in advance for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload