Data Acquisition Toolbox

Logging Data to Disk

This example shows how to configure an analog input object for data logging and shows an example for logging multiple triggers to disk.

DEMOAI_LOGGING illustrates the functionality of DAQREAD by reading the data logged into the MATLAB® workspace. Examples are given on how DAQREAD can be used to read all the data logged to disk, a portion of the data logged to disk, the event information that occurred while the object was being logged to disk and the state of the data acquisition object when logging was finished.


Note: This can only be run using the 32-bit version of MATLAB® and Data Acquisition Toolbox™. To learn more about using data acquisition devices on other platforms, see this example.

First, find any running data acquisition objects and stop them. This stops all running data acquisition objects from interfering with this example. This code is usually not necessary outside this example unless there are multiple data acquisition objects running.

if (~isempty(daqfind))

DATA LOGGING. In this example, you will learn about logging data to a file and reading the data back into the MATLAB workspace.

To get started, an analog input object containing two channels is created.

ai = analoginput('winsound');
ch = addchannel(ai, [1 2]);

Note there are four properties that control how data is logged to disk:

   |Logging| -- type |daqhelp Logging| for more info
   |LogFileName| -- type |daqhelp LogFileName| for more info
   |LoggingMode| -- type |daqhelp LoggingMode| for more info
   |LogToDiskMode| -- type |daqhelp LogToDiskMode| for more info

With the following settings, data will be logged to a temporary file and to memory. If another start occurs, the new data will overwrite the old data in the file.

ai.LogFileName = tempname();
ai.LogToDiskMode = 'overwrite';
ai.LoggingMode = 'Disk&Memory';

Let's configure the analog input object to trigger five times. Each trigger will collect 1000 data samples using a sample rate of 10,000 samples per second.

ai.TriggerType = 'Immediate';
ai.TriggerRepeat = 4;
ai.SamplesPerTrigger = 1000;
ai.SampleRate = 11025;

Since the TriggerType is 'Immediate', the Logging property will be immediately set to 'On' after the analog input object is started with the START command. Therefore, the data will be immediately sent to the log file upon starting. Then, we'll wait for up to 2 seconds for the acquisition to complete.

ans =

The DAQREAD command is used to read the .daq file. With the following syntax, all the samples logged to disk will be read into the variable data. data will be an m-by-n matrix where m is the number of samples logged and n is the number of channels. However, if multiple triggers are logged, the data between triggers are separated by NaNs. In this case, m will be increased by the number of triggers. The time vector will contain the relative times that each data sample was logged relative to the first trigger.

[data, time] = daqread(ai.LogFileName);

% Now the data can be displayed:

It is possible to limit the amount of data being read by DAQREAD by using one of the following properties:

  Samples            Specify the range of samples
  Triggers           Specify the range of triggers
  Time               Specify the range of time in seconds
  Channels           Specify the channel indices

For example the following command will read all samples between the 1000th data sample and the 2000th data sample.

[data,time] = daqread(ai.LogFileName, 'Samples', [1000 2000]);

% The subset of data is now plotted.

Similarly, you can restrict the number of Channels read by specifying the 'Channels' property. For example, to read samples 500 to 1000 of the second channel, you would assign the following values to the 'Samples' and 'Channels' properties.

[data,time] = daqread(ai.LogFileName, 'Samples', [500 1000], 'Channels', 2);

% The subset of data is now plotted.

You can select the format of the data and time output variables using the following properties where the default value is given in curly braces {}.

  DataFormat                    {double} | native
  TimeFormat                    {vector} | matrix

For example, to read in all the data in native format for the 1st channel only, you would assign the following values to the 'Channels' and 'DataFormat' properties.

[data,time] = daqread(ai.LogFileName, 'Channels', 1, 'DataFormat', 'native');

% The subset of data is now plotted.

You can also use the DAQREAD command to read in information related to the object used to log the data and hardware information.

daqinfo = daqread(ai.LogFileName, 'info');

To obtain the object information:

ans = 
            BitsPerSample: 16
          BufferingConfig: [1024 5]
            BufferingMode: 'Auto'
                  Channel: [1x2 struct]
              ChannelSkew: 0
          ChannelSkewMode: 'None'
              ClockSource: 'Internal'
                 EventLog: [1x7 struct]
       InitialTriggerTime: [2006 7 27 13 44 4.49803328514099]
                InputType: 'AC-Coupled'
              LogFileName: 'C:\Temp\tp440420.daq'
                  Logging: 'Off'
              LoggingMode: 'Disk&Memory'
            LogToDiskMode: 'Overwrite'
        ManualTriggerHwOn: 'Start'
                     Name: 'winsound0-AI'
                  Running: 'Off'
               SampleRate: 11025
          SamplesAcquired: 5000
         SamplesAvailable: 5000
        SamplesPerTrigger: 1000
      StandardSampleRates: 'On'
                      Tag: ''
                  Timeout: 1
              TimerPeriod: 0.1
           TriggerChannel: []
         TriggerCondition: 'None'
    TriggerConditionValue: 0
             TriggerDelay: 0
        TriggerDelayUnits: 'Seconds'
            TriggerRepeat: 4
         TriggersExecuted: 5
              TriggerType: 'Immediate'
                     Type: 'Analog Input'

To obtain channel information:

ans = 
1x2 struct array with fields:

To obtain event information you can use the SHOWDAQEVENTS command to display the event information.

   1 Start               ( 13:44:04, 0 )
   2 Trigger#1           ( 13:44:04, 0 )         Channel: N/A
   3 Trigger#2           ( 13:44:04, 1000 )      Channel: N/A
   4 Trigger#3           ( 13:44:04, 2000 )      Channel: N/A
   5 Trigger#4           ( 13:44:04, 3000 )      Channel: N/A
   6 Trigger#5           ( 13:44:04, 4000 )      Channel: N/A
   7 Stop                ( 13:44:04, 5000 )

To obtain the hardware information:

ans = 
                AdaptorName: 'winsound'
                       Bits: 16
                   Coupling: {'AC Coupled'}
                 DeviceName: 'AK5370          '
            DifferentialIDs: []
                      Gains: []
                         ID: '0'
                InputRanges: [-1 1]
              MaxSampleRate: 44100
              MinSampleRate: 8000
             NativeDataType: 'int16'
                   Polarity: {'Bipolar'}
                 SampleType: 'SimultaneousSample'
             SingleEndedIDs: [1 2]
              SubsystemType: 'AnalogInput'
              TotalChannels: 2
    VendorDriverDescription: 'Windows Multimedia Driver'
        VendorDriverVersion: '0.1'

Lastly, clean up the data acquisition object