| Data Acquisition Toolbox | Search  Help Desk |
Example: Logging and Retrieving Information
This section illustrates how to log information to a disk file and then return the logged information to MATLAB using various calls todaqread.
In the example below, a sound card is configured for stereo acquisition, data is logged to memory and to a disk file, four triggers are issued, and two seconds of data is collected for each trigger at a sample rate of 8 kHz. All the logged data is then read from the file as sample-time pairs.
Initialization: Create the analog input object ai for a sound card. The available adaptors are found with daqhwinfo.
Configuration: Add two hardware channels toai = analoginput('winsound');%ai = analoginput('nidaq', 1);
ai, define a two second acquisition for each trigger, set the trigger to repeat three times, and log data and events to the file file00.daq.
ch = addchannel(ai, 1:2); %ch = addchannel(ai, 0:1); duration = 2; % Two seconds of data for each trigger set(ai, 'SampleRate', 8000); ActualRate = get(ai, 'SampleRate'); set(ai, 'SamplesPerTrigger', duration*ActualRate); set(ai, 'TriggerRepeat', 3); set(ai, 'LogFileName', 'file00.daq'); set(ai, 'LogToDiskMode', 'Overwrite'); set(ai, 'LoggingMode', 'Disk&Memory');Execution: Start
ai, wait for ai to stop running, and extract all the data stored in the engine as sample-time pairs.
start(ai)
while strcmp(ai.Running, 'On')
end
[data, time] = daqread('file00.daq');
Termination: Plot the data and delete ai.
subplot(211); plot(data);
title(sprintf('daqdoc4\\_9\nLogging and Retrieving Data'))
xlabel('Samples'); ylabel('Volts')
subplot(212); plot(time, data);
xlabel('Time (seconds)'); ylabel('Signal (Volts)')
delete(ai)
Returning Data Based on Samples
You can return data based on samples using theSamples property. The daqread call shown below returns samples 1000 to 2000 for both sound card channels.
[data, time] = daqread('file00.daq', 'Samples', [1000 2000]);
figure; subplot(211); plot(data);
xlabel('Samples'); ylabel('Volts')
subplot(212); plot(time, data);
xlabel('Time (seconds)'); ylabel('Volts')
Returning Data Based on Channels
You can return data based on channels using theChannels property. The daqread call shown below returns samples 1000 to 2000 for the second sound card channel.
[data, time] = daqread('file00.daq', 'Samples', [1000 2000],
'Channels', 2);
figure; subplot(211);plot(data);
xlabel('Samples'); ylabel('Volts')
subplot(212); plot(time, data);
xlabel('Time (seconds)'); ylabel('Volts')
Alternatively, you can specify the channel name.
[data, time] = daqread('file00.daq', 'Samples', [1000 2000],
'Channels', {'Right'});
Returning Data Based on Triggers
You can return data based on triggers using theTriggers property. The daqread call shown below returns all the data associated with the second and third triggers for both sound card channels.
[data, time] = daqread('file00.daq', 'Triggers', [2 3]);
figure; subplot(211); plot(data);
xlabel('Samples'); ylabel('Volts')
subplot(212); plot(time, data);
xlabel('Time (seconds)'); ylabel('Volts')
Returning Data Based on Time
You can return data based on time using theTime property. The values specified for Time must be datenum values.
For this example, the first 25% of the data acquired for the first trigger will be returned. The first Time value corresponds to the first trigger event and can be found with the EventLog property.
events = ai.EventLog data = events(2).Data; time1 = data.TimeStamp;The second
Time value is constructed by adding one-half second to the first Time value. Note that when a number is added to a datenum value, it must be normalized as shown below.
time2 = time1 + 0.5/(24*60*60);You can now return the first 25% of the data acquired for the first trigger.
[data, time] = daqread('file00.daq', 'Time', [time1 time2]);
figure; subplot(211); plot(data);
xlabel('Samples'); ylabel('Volts')
subplot(212); plot(time, data);
xlabel('Time (seconds)'); ylabel('Volts')
Returning Event, Object, and Hardware Information
As shown on page 4-65, you can return event, object, and hardware, information by specifying the appropriate output arguments todaqread. To return all event information, you must return all the logged data.
[data, time, abstime, events, info] = daqread('file00.daq');
{events.Type}
ans =
'Start' 'Trigger' 'Trigger' 'Trigger' 'Trigger' 'Stop'
If part of the data is returned, then only the events associated with the requested data are returned.
[data, time, abstime, events, info] = daqread('file00.daq',
'Trigger', [1 3]);
{events.Type}
ans =
'Trigger' 'Trigger' 'Trigger'
Alternatively, you can return the entire event log as well as object and hardware information by including 'info' as an input argument to daqread.
daqinfo = daqread('file00.daq', 'info')
hwinfo = daqinfo.HwInfo
objinfo = daqinfo.ObjInfo
eventinfo = daqinfo.ObjInfo.EventLog