This example shows how to log a data acquisition session. There are a number of reasons to do this: acquiring large data sets that would not normally fit into memory, or acquiring data samples that we plan on examining at a later time.
Note: This can only be run using the 32-bit version of MATLAB® and Data Acquisition Toolbox™. To learn more about using data acquisition devices on other platforms, see this example.
Before acquiring any data, you must create an analog input object and specify the channels from which to acquire data. You may need to modify the following code to match your acquisition hardware.
ai = analoginput('winsound'); addchannel(ai, 1);
Configure the analog input object for acquisition. The configuration we will use is:
1000 samples per trigger
repeat the trigger 20 times, for a total of 21 triggers
A sample rate of 44100 samples per second
ai.SamplesPerTrigger = 1000; ai.TriggerRepeat = 20; ai.SampleRate = 44100;
Now configure the acquisition to log to disk.
ai.LoggingMode = 'Disk';
When logging to disk, you can specify what file to write to. To alter the name of the log file, set the
ai.LogFileName = 'LogFile.daq';
You can now begin your acquisition.
% Start the acquisition and wait for it to finish start(ai); wait(ai, 2);
When our data acquisition is complete, extract the data samples you have collected.
data = daqread('LogFile.daq'); plot(data);
You can also obtain a time vector that represents the time stamp of each data sample relative to the first trigger.
[data, time] = daqread('LogFile.daq');
In addition, you can obtain the hardware settings that were used during acquisition. This returns a structure composed of
two fields containing all the hardware information at the time of acquisition. To obtain this, simply pass in the logged
data file name with the
info parameter to
info = daqread('LogFile.daq', 'info')
info = ObjInfo: [1x1 struct] HwInfo: [1x1 struct]
So far you have seen only how to look at all the data collected. By using different parameter-value pairs, you can look at
the data through a number of different constraints. This is useful when you are interested in only a portion of the data collected
while working with large data files. For example, you can request only those samples that fall within a certain sample range.
To do this, pass in the
samples keyword along with the range of samples you want to
data = daqread('LogFile.daq', 'samples', [100 1200]); plot(data)
You can request the data that occurred only in a specific time frame. To do this you use
daqread with the
time keyword paired with a time vector (in seconds).
[data, time] = daqread('LogFile.daq', 'time', [.1 .25]); plot(time, data);
You can request the data that occurred only within a range of triggers. To do this, use
daqread with the
triggers keyword paired with either a trigger range or a single trigger.
data = daqread('LogFile.daq', 'triggers', [6 8]); plot(data);
With these capabilities,
daqread can be used to log data and acquisition information, as well as work with large data sets by extracting and operation on
a subset of logged data at a time.