Generate Signals in the Background Continuously

This example shows how to continuously generate signals. A continuous background generation depends on events and listeners to allow your code to enable continuous queuing of data and to react to any errors as they occur. For details, see Events and Listeners — Concepts in the MATLAB Object-Oriented Programming documentation. In this example, you generate from an NI 9263 device with ID cDAQ1Mod2 using a listener on the DataRequired event.

Listeners execute a callback function when notified that the event has occurred. Use Session.addlistener to create the listener object that executes your callback function.

Create an NI session object and add an analog output 'Voltage' channel on cDAQ1Mod2:

s = daq.createSession('ni');
addAnalogOutputChannel(s,'cDAQ1Mod2', 'ao0', 'Voltage');

Specify the channel ID on NI devices using a terminal name, like 'ao1', or a numeric equivalent like 1.

Create the data to output and queue the output data.

queueOutputData(s,linspace(1, 10, 1000)');

Add the listener to the DataRequired event and assign it to the variable lh:

lh = addlistener(s,'DataRequired',@queueMoreData);

Step 4. Create a simple callback function to generate the data and save it as queueMoreData.m in your working folder:

  function queueMoreData(src,event)
        queueOutputData(s,linspace(1, 10, 1000)');
  end

Generate the signal:

startBackground(s);

You can execute other MATLAB® commands while the generation is in progress. In this example, issue a pause(), which causes the MATLAB command line to wait for you to press any key.

pause();

Delete the listener:

delete(lh)

Related Topics