Data Acquisition Toolbox | ![]() ![]() |
Example: Logging and Retrieving Information
This example illustrates how to log information to a disk file and then retrieve the logged information to MATLAB using various calls to daqread
.
A sound card is configured for stereo acquisition, data is logged to memory and to a disk file, four triggers are issued, and 2 seconds of data are collected for each trigger at a sampling rate of 8 kHz. You can run this example by typing daqdoc8_1
at the MATLAB command line.
1. Create a device object - Create the analog input object ai
for a sound card. The installed adaptors and hardware IDs are found with daqhwinfo
.
ai = analoginput('winsound'); %ai = analoginput('nidaq',1); %ai = analoginput('cbi',1);
2. Add channels - Add two hardware channels to ai
.
ch = addchannel(ai,1:2); %ch = addchannel(ai,0:1); % For NI and CBI
3. Configure property values - Define a 2 second acquisition for each trigger, set the trigger to repeat three times, and log information to the file file00.daq
.
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,'LoggingMode','Disk&Memory')
4. Acquire data - Start ai
, wait for ai
to stop running, and extract all the data stored in the log file as sample-time pairs.
start(ai) while strcmp(ai.Running,'On') end [data,time] = daqread('file00.daq');
Plot the data and label the figure axes.
subplot(211), plot(data) title('Logging and Retrieving Data') xlabel('Samples'), ylabel('Signal (Volts)') subplot(212), plot(time,data) xlabel('Time (seconds)'), ylabel('Signal (Volts)')
5. Clean up - When you no longer need ai
, you should remove it from memory and from the MATLAB workspace.
delete(ai) clear ai
Retrieving Data Based on Samples
You can retrieve data based on samples using the Samples
property. To retrieve samples 1000 to 2000 for both sound card channels
[data,time] = daqread('file00.daq','Samples',[1000 2000]);
Plot the data and label the figure axes.
subplot(211), plot(data); xlabel('Samples'), ylabel('Signal (Volts)') subplot(212), plot(time,data); xlabel('Time (seconds)'), ylabel('Signal (Volts)')
Retrieving Data Based on Channels
You can retrieve data based on channels using the Channels
property. To retrieve samples 1000 to 2000 for the second sound card channel
[data,time] = daqread('file00.daq','Samples',[1000 2000], 'Channels',2);
Plot the data and label the figure axes.
subplot(211), plot(data); xlabel('Samples'), ylabel('Signal (Volts)') subplot(212), plot(time,data); xlabel('Time (seconds)'); ylabel('Signal (Volts)')
Alternatively, you can retrieve data for the second sound card channel by specifying the channel name.
[data,time] = daqread('file00.daq','Samples',[1000 2000], 'Channels',{'Right'});
Retrieving Data Based on Triggers
You can retrieve data based on triggers using the Triggers
property. To retrieve all the data associated with the second and third triggers for both sound card channels
[data,time] = daqread('file00.daq','Triggers',[2 3]);
Plot the data and label the figure axes.
subplot(211), plot(data); xlabel('Samples'), ylabel('Signal (Volts)') subplot(212), plot(time,data); xlabel('Time (seconds)'), ylabel('Signal (Volts)')
Retrieving Data Based on Time
You can retrieve data based on time using the Time
property. Time
must be specified in seconds and Time=0
corresponds to the first logged sample. To retrieve the first 25% of the data acquired for the first trigger
[data,time] = daqread('file00.daq','Time',[0 0.5]);
Plot the data and label the figure axes.
subplot(211), plot(data); xlabel('Samples'), ylabel('Signal (Volts)') subplot(212), plot(time, data); xlabel('Time (seconds)'), ylabel('Signal (Volts)')
Retrieving Event, Object, Channel, and Hardware Information
You can retrieve event, object, channel, and hardware information by specifying the appropriate arguments to daqread
. For example, to retrieve 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 you retrieve part of the data, 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'
You can retrieve 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') daqinfo = ObjInfo: [1x1 struct] HwInfo: [1x1 struct]
To return the event log information
eventinfo = daqinfo.ObjInfo.EventLog eventinfo = 6x1 struct array with fields: Type Data
![]() | Retrieving Logged Information | Reference | ![]() |