Search code examples
callbackmatlabdata-acquisition

Continuously acquire data in MATLAB using callbacks and persistent variables


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.

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:

  1. set my 'SamplesPerTrigger' to Inf
  2. set the 'TimerPeriod' to some value to trigger the 'TimerFcn'
  3. set the 'TimerFcn' to some subfunction callback which appends data to a persistent variable

Is this a correct way to do this?

My code is as follows:

  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

The initial analog input is definitely working properly. I have tried many permutations of giving the AcquireData callback to 'TimerFcn'. The error I receive is

`??? Error using ==> acquire_arena_test>AcquireData Too many input arguments.

Warning: The TimerFcn callback is being disabled. To enable the callback, set the TimerFcn property. `

Thanks in advance for any help.


Solution

  • Sorry about answering my own question, but I figured it out. The trigger was not needed after all. Using a national instruments board (or a sound card, as it turns out) you can just change the LoggingMode to 'disk' and specify a file to save the .daq (data acquisition toolbox) file to save as with LogFileName. If you want to use the memory on your board, change the mode to disk&Memory. Helpful document:

    http://www.mathworks.com/help/toolbox/daq/f12-16658.html

    The script below acquires data during the pause, which is as long as you want it to be..

    daqreset;
    clear all;
    ai = analoginput('nidaq','Dev1');
    chans = addchannel(ai,0:6);
    set(ai,'SamplesPerTrigger',Inf);
    set(ai,'SampleRate',1000)
    set(ai,'LogToDiskMode','Overwrite')
    set(ai,'LogFileName','log.daq')
    set(ai,'LoggingMode', 'disk') 
    
    start(ai)
    
    pause()
    stop(ai)
    
    data = daqread('log.daq');
    delete(ai);
    

    Note that you still need to set 'SamplesPerTrigger' to Inf for this to work properly. Thank you to Jonas for his help as well.