Log Analog Input Data to a File Using NI Devices

This example shows how to save data acquired in the background to a file. Use the session-based interface and acquires analog input data using non-blocking commands. If you are using the legacy interface, refer to "Logging Data To Disk" in the Analog Input examples list

Create a Session with Analog Input Channels

Create a session and add two analog input channels with Voltage measurement type. For this example use National Instruments® X Series data acquisition device, NI PCIe-6363 card with ID Dev1.

d = daq.getDevices;
s = daq.createSession('ni');
addAnalogInputChannel(s,'Dev1',0:1, 'Voltage');
s = 

Data acquisition session using National Instruments hardware:
   Will run for 1 second (1000 scans) at 1000 scans/second.
   Number of channels: 2
      index Type Device Channel MeasurementType      Range       Name
      ----- ---- ------ ------- --------------- ---------------- ----
      1     ai   Dev1   ai0     Voltage (Diff)  -10 to +10 Volts
      2     ai   Dev1   ai1     Voltage (Diff)  -10 to +10 Volts

Create a Log File

Create the file log.bin and open it. You will write the acquired data to this file in binary format. Save the file identifier in the variable fid1.

fid1 = fopen('log.bin','w');

Add a Listener

When you acquire data in the background, you can provide the acquisition session directions to handle the incoming data, using listeners and events. A DataAvailable event occurs when a specific amount of data is available to the session. A listener can respond to that event and initiate specified function.

Use addlistener to add an anonymous function to the session. This function is called every time the DataAvailable event occurs, and logs the acquired data to a file. By default this listener is called 10 times per second.

The anonymous function logData requires three inputs:

  • src- source of the event

  • event- name of the event

  • fid1- file identifier for logging data

When you add a listener, a handle to the listener is returned. Save the handle in the variable lh and delete it later.

lh = addlistener(s,'DataAvailable',@(src, event)logData(src, event, fid1));

Acquire Data in the Background

Acquire data continuously in a non-blocking mode.

s.IsContinuous = true;

Continuous background acquisition runs in parallel with other operations on MATLAB®. You can execute MATLAB commands while the acquisition is running. For the purpose of this example, simulate a long computation by calling pause.


Stop the Continuous Acquisition and Close the Log File

Explicitly call stop after 5 seconds to end the background acquisition and delete the listener.

  • Close the log file.


Load Data from the Log File

  • Read the log file.

  • Load file contents as a 3 column matrix into data.

  • Close the log file.

fid2 = fopen('log.bin','r');
[data,count] = fread(fid2,[3,inf],'double');

Plot the Data

  • Assign each column of data to the following variables:

  • t- time

  • ch- channel 0 and channel 1

  • Plot ch against t.

t = data(1,:);
ch = data(2:3,:);
plot(t, ch);

Write a Function to Log the Data.

Create a multi-line function logData and store in a separate MATLAB file.

type logData.m
function logData(src, evt, fid)
% Add the time stamp and the data values to data. To write data sequentially,
% transpose the matrix.

%   Copyright 2011 The MathWorks, Inc.

data = [evt.TimeStamps, evt.Data]' ;