title: How to avoid a Data Missed event in matlab when doing data acquisition from an NI board?
body: <p>I am receiving DataMissed event(s) in matlab when acquiring waveform data (2 channels) and an analog input trigger (set up in pretrigger mode - negative delay value).</p>
  
  <p>Here is the code:</p>
  
  <pre><code>sampleRate = 500000;
  pretrigger = 8;
  trigger_value = 0.6;
  ai = analoginput('nidaq','Dev1');
  ch = addchannel(ai,0:channels);
  set(ai,'SampleRate',sampleRate);
  requiredSamples = floor(pretrigger*sampleRate);
  set(ai,'SamplesPerTrigger',requiredSamples);
  
  %triggering
  set(ai,'TriggerChannel',ch(channels+1));
  set(ai,'TriggerType','Software');
  set(ai,'TriggerCondition','Rising');
  set(ai,'TriggerConditionValue',trigger_value);
  set(ai,'TriggerDelay',-pretrigger);
  set(ai, 'TriggerRepeat', 0);
  
  set(ai,'LoggingMode','Disk');
  set(ai,'LogToDiskMode','Index');
  set(ai,'LogFileName',[filename '_01']);
  
  waitTime = pretrigger * 1.1 + 0.5;
  
  for k = 1:100
    start(ai);
    CurrentFile = get(ai,'LogFileName');
    disp(['Acquiring for ' CurrentFile]);
    while strcmp((get(ai, 'Running')), 'On')
      pause(1);
    end
    wait(ai,waitTime);
    showdaqevents(ai)
    disp(['Triggered ' CurrentFile]);
    while strcmp((get(ai, 'Logging')), 'On'); end;
    disp(['Saved ' CurrentFile]);
  end
  
  stop(ai);
  delete(ai);
  clear ai;
  </code></pre>
  
  <p>When running this code, I get Data Missed Events after the trigger event is received (using the showdaqevents(ai)).  However, the Data Missed Events have a sample number of NA, so I'm not completely certain that they are coming after the trigger.</p>
  
  <p>Here's an example of the showdaqevents output:</p>
  
  <pre><code>1 Start               ( 14:55:35, 0 )
  2 Trigger#1           ( 14:56:02, 0 )      Channel: [3]
  3 DataMissed          (    N/A  , 13959168 ) 
  4 DataMissed          (    N/A  , 13959168 ) 
  5 DataMissed          (    N/A  , 13959168 ) 
  6 DataMissed          (    N/A  , 13959168 ) 
  7 DataMissed          (    N/A  , 13959168 ) 
  8 DataMissed          (    N/A  , 13959168 ) 
  9 Stop                ( 14:56:03, 5 )
  </code></pre>
  
  <p>Looking into Data Missed Events in the matlab <a href="http://www.mathworks.com/help/toolbox/daq/ref/datamissedfcn.html" rel="nofollow">documentation</a>, there are two explanations given: </p>
  
  <ul>
  <li>The engine cannot keep up with the rate of acquisition.   </li>
  <li>The driver wrote new data into the hardware's FIFO buffer before the
  previously acquired data was read. You can usually avoid this problem
  by increasing the size of the memory block with the BufferingConfig
  property.</li>
  </ul>
  
  <p>I've attempted to increase the memory available with the daqmem command, but I haven't found that to work.  500000 samples/second, 3 channels is about 90 MB total per trigger, and daqmem reports having over 360MB free (though the buffermode is set to Auto).  I am wondering if I need to periodically call getdata(ai) in the while loop to clear the memory.  The other possibility is that I am running at too high of a sample rate for the system?</p>
  
  <p>The hardware:</p>
  
  <ul>
  <li>Dual core CPU (Athlon X2 3200), 4GB of RAM, 500 GB SATA hard disk</li>
  <li><a href="http://sine.ni.com/nips/cds/view/p/lang/en/nid/201939" rel="nofollow">NI PCI-6122</a></li>
  </ul>
  
  <p>I should mention that the likelihood of getting a DataMissed event goes up the longer (in time) I let the system acquire before pressing the trigger.</p>
  
  <p>Is there a problem with my data acquisition code (I'm most uncertain about having the pause in there in the middle of the acquisition)? The hardware? Is it something not to worry about?  Is Visual Studio better for dealing with NI boards?</p>