Create a Device Object

Understanding Device Objects

Device objects are the toolbox components you use to access your hardware device. They provide a gateway to the functionality of your hardware, and allow you to control the behavior of your data acquisition application. Each device object is associated with a specific hardware subsystem.

To create a device object, you call functions called object creation functions (or object constructors). These functions are implemented using the object-oriented programming capabilities provided by the MATLAB software, which are described in Object-Oriented Programming documentation. The device object creation functions are listed below.

Device Object Creation Functions

Function

Description

analoginput

Create an analog input object.

analogoutput

Create an analog output object.

digitalio

Create a digital I/O object.

Before you can create a device object, the associated hardware driver adaptor must be registered. Adaptor registration occurs automatically. However, if for some reason an adaptor is not automatically registered, then you must do so manually with the daqregister function. Refer to Hardware and Device Drivers for more information.

You can find out how to create device objects for a particular vendor and subsystem with the ObjectConstructorName field of the daqhwinfo function. For example, to find out how to create an analog input object for an installed National Instruments® board, you supply the appropriate Adaptor-Specific Information to daqhwinfo.

out = daqhwinfo('nidaq');
out.ObjectConstructorName(:)
ans = 
    'analoginput('nidaq','Dev1')'
    'analogoutput('nidaq','Dev1')'
    'digitalio('nidaq','Dev1')'

The constructor syntax tells you that you must supply the adaptor name and the hardware ID to the analoginput function

ai = analoginput('nidaq','Dev1');

The association between device objects and hardware subsystems is shown below.

Create an Array of Device Objects

In the MATLAB workspace, you can create an array from existing variables by concatenating those variables together. The same is true for device objects. For example, suppose you create the analog input object ai and the analog output object ao for a sound card:

ai = analoginput('winsound');
ao = analogoutput('winsound');

You can now create a device object array consisting of ai and ao using the usual MATLAB syntax. To create the row array x:

x = [ai ao]
   Index:    Subsystem:         Name:
   1         Analog Input       winsound0-AI
   2         Analog Output      winsound0-AO

To create the column array y:

y = [ai;ao];

Note that you cannot create a matrix of device objects. For example, you cannot create the matrix

z = [ai ao;ai ao];
??? Error using ==> analoginput/vertcat
Only a row or column vector of device objects can be created.

Depending on your application, you might want to pass an array of device objects to a function.

x.SampleRate = 44100)

Refer to the functions to see which one accept a device object array as an input argument.

Where Do Device Objects Exist?

When you create a device object, it exists in both the MATLAB workspace and the data acquisition engine. For example, suppose you create the analog input object ai for a sound card and then make a copy of ai.

ai = analoginput('winsound');
newai = ai;

The copied device object newai is identical to the original device object ai. You can verify this by setting a property value for ai and returning the value of the same property from newai.

ai.SampleRate = 22050
ans =
       22050

As shown below, ai and newai return the same property value because they both reference the same device object in the data acquisition engine.

If you delete either the original device object or a copy, then the engine device object is also deleted. In this case, you cannot use any copies of the device object that remain in the workspace because they are no longer associated with any hardware. Device objects that are no longer associated with hardware are called invalid objects. The example below illustrates this situation.

delete(ai);
newai
newai =
Invalid Data Acquisition object.
This object is not associated with any hardware and
should be removed from your workspace using CLEAR.

You should remove invalid device objects from the workspace with the clear command.

Was this topic helpful?