Add Channels to an Analog Input Object

Channel Group

After creating the analog input object, you must add hardware channels to it. As shown by the figure in Hardware Channels or Lines, you can think of a device object as a container for channels. The collection of channels contained by the device object is referred to as a channel group. As described in Hardware Channel IDs to the MATLAB Indices, a channel group consists of a mapping between hardware channel IDs and MATLAB® indices (see below).

When adding channels to an analog input object, you must follow these rules:

  • The channels must reside on the same hardware device. You cannot add channels from different devices, or from different subsystems on the same device.

  • The channels must be sampled at the same rate.

You add channels to an analog input object with the addchannel function. addchannel requires the device object and at least one hardware channel ID as input arguments. You can optionally specify MATLAB indices, descriptive channel names, and an output argument. For example, to add two hardware channels to the device object ai created in the preceding section:

chans = addchannel(ai,0:1);

The output argument chans is a channel object that reflects the channel array contained by ai. You can display the class of chans with the whos command.

whos chans
Name        Size         Bytes  Class

  chans       2x1            512  aichannel object

Grand total is 7 elements using 512 bytes

You can use chans to easily access channels. For example, you can easily configure or return property values for one or more channels. As described in Reference Individual Hardware Channels, you can also access channels with the Channel property.

Once you add channels to an analog input object, the properties listed below are automatically assigned values. These properties provide descriptive information about the channels based on their class type and ID.

Descriptive Analog Input Channel Properties

Property Name

Description

HwChannel

Specify the hardware channel ID.

Index

Indicate the MATLAB index of a hardware channel.

Parent

Indicate the parent (device object) of a channel.

Type

Indicate a channel.

If you are using scanning hardware, then the MATLAB indices define the scan order; index 1 is sampled first, index 2 is sampled second, and so on.

    Note   The number of channels you can add to a device object depends on the specific board you are using. Some boards support adding channels in any order and adding the same channel multiple times, while other boards do not. Additionally, each channel might have its own input range, which is verified with each acquired sample. The collection of channels you add to a device object is sometimes referred to as a channel gain list or a channel gain queue. For scanning hardware, these channels define the scan order.

Reference Individual Hardware Channels

As described in the preceding section, you can access channels with the Channel property or with a channel object. To reference individual channels, you must specify either MATLAB indices or descriptive channel names.

MATLAB Indices

Every hardware channel contained by an analog input object has an associated MATLAB index that is used to reference the channel. When adding channels with the addchannel function, index assignments can be made automatically or manually. In either case, the channel indices start at 1 and increase monotonically up to the number of channel group members.

For example, the analog input object ai created in the preceding section had the MATLAB indices 1 and 2 automatically assigned to the hardware channels 0 and 1, respectively. To manually swap the hardware channel order, you supply the appropriate index to chans and use the HwChannel property.

chans(1).HwChannel = 1;
chans(2).HwChannel = 0;

Alternatively, you can use the Channel property.

ai.Channel(1).HwChannel = 1;
ai.Channel(2).HwChannel = 0;

Note that you can also use addchannel to specify the required channel order.

chans = addchannel(ai,[1 0]);

Descriptive Channel Names

Choosing a unique, descriptive name can be a useful way to identify and reference channels — particularly for large channel groups. You can associate descriptive names with hardware channels using the addchannel function. For example, suppose you want to add 16 single-ended channels to ai, and you want to associate the name TrigChan with the first channel in the group.

ai.InputType = 'SingleEnded';
addchannel(ai,0,'TrigChan');
addchannel(ai,1:15);

Alternatively, you can use the ChannelName property.

ai.InputType = 'SingleEnded';
addchannel(ai,0:15);
ai.Channel(1).ChannelName = 'TrigChan';

You can now use the channel name to reference the channel.

ai.TrigChan.InputRange = [-10 10];

Add Channels for a Sound Card

Suppose you create the analog input object ai for a sound card.

ai = analoginput('winsound');

Most sound cards have just two hardware channels that you can add. If one channel is added, the sound card is said to be in mono mode. If two channels are added, the sound card is said to be in stereo mode. However, the rules for adding these two channels differ from those of other data acquisition devices. These rules are described below.

Mono Mode

If you add one channel to ai, the sound card is said to be in mono mode and the channel added must have a hardware ID of 1.

addchannel(ai,1);

At the software level, mono mode means that data is acquired from channel 1. At the hardware level, you generally cannot determine the actual channel configuration and data can be acquired from channel 1, channel 2, or both depending on your sound card. Channel 1 is automatically assigned the descriptive channel name Mono.

ai.Channel.ChannelName
ans =
Mono

Stereo Mode

If you add two channels to ai, the sound card is said to be in stereo mode. You can add two channels using two calls to addchannel provided channel 1 is added first.

addchannel(ai,1);
addchannel(ai,2);

Alternatively, you can use one call to addchannel provided channel 1 is specified as the first element of the hardware ID vector.

chan = addchannel(ai,1:2);

Stereo mode means that data is acquired from both hardware channels. Channel 1 is automatically assigned the descriptive name Left and channel 2 is automatically assigned the descriptive name Right.

chan.ChannelName
ans = 
    'Left'
    'Right'

While in stereo mode, if you want to delete one channel, then that channel must be channel 2. If you try to delete channel 1, an error is returned.

delete(ai.Channel(2))

The sound card is now in mono mode.

Was this topic helpful?