| Data Acquisition Toolbox | Search  Help Desk |
Reference by Channel Name
In some circumstances it may be useful to reference one or more channels by name rather than by index. Names are associated with channels through theChannelName property. You can assign channel names through the addchannel function or by directly assigning values to ChannelName.
For example, suppose you want TrigChan to be the name of the first channel in myAI's channel group. After naming the channel, you can configure its property values by referencing the channel by its name. This feature is illustrated in the example below.
Initialization: Create the analog input object myAI for a sound card. The installed adaptors and hardware ID's are found with daqhwinfo.
myAI = analoginput('winsound');
%myAI = analoginput('nidaq', 1);
Configuration: Add two hardware channels to myAI, define an eight second acquisition duration, copy channel 1 to the variable chan1, assign a name to channel 1, and set a property value by referencing the channel name. The actual sampling rate is retrieved since it may be set to a value that differs from the specified value.
addchannel(myAI, 1:2); %addchannel(myAI, 0:1); duration = 8; ActualRate = get(myAI, 'SampleRate'); chan1 = myAI.Channel(1);Execution: Startset(chan1, 'ChannelName', 'TrigChan');set(myAI, 'SamplesPerTrigger', duration*ActualRate);set(myAI.TrigChan, 'UnitsRange', [0 100]); myAI.TrigChan.Units = 'dB'
myAI and wait for the device object to stop running. Eight seconds of data is collected for each channel contained by myAI.
start(myAI) while strcmp(myAI.Running, 'On') endTermination: Display summary information and delete
myAI.
myAI delete(myAI)
Alternatively, you can define a channel name with the addchannel function.
Note:addchannel(myAI, 0, {'TrigChan'});addchannel(myAI, 1:4);
makenames function. This function is described below.
The makenames Function
You can generate a list of channel names with themakenames function.
names = makenames('prefix', index);
names is a column cell array containing the channel names.
prefix is a string which constitutes the first part of the channel name.
index are indices for the channel name which are appended to the end of the prefix string. index must consist of nonnegative integers.
myAI and assign the names Chan6, Chan7, and Chan8 to these new channels. You can create the channel names using makenames and then pass the resulting cell array to addchannel.
Alternatively, you can assign names to channels after they have been added to the object.names = makenames('Chan', 6:8);addchannel(myAI, 5:7, names);
addchannel(myAI, 5:7);names = makenames('Chan', 6:8);set(myAI.Channel(6:8), {'ChannelName'}, names);