Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Saving and Loading Data Acquisition Objects

This example shows how to save data acquisition session between MATLAB® sessions. This example gives two methods for saving your data acquisition session setup.

The first method is to use the SAVE command to save the data acquisition objects to a MAT-file and the LOAD command to load the data acquisition objects from the MAT-file back into the MATLAB workspace. This portion of the example illustrates how more than one object in the MATLAB workspace can point to the same object in the data acquisition engine and how loading a data acquisition object that currently exists in the data acquisition engine can effect data acquisition objects that exist in the MATLAB workspace.

The second method is to use the OBJ2MFILE command to convert the data acquisition object to the equivalent MATLAB code to create the object. The data acquisition object can be recreated in a later MATLAB session by running the MATLAB file created by OBJ2MFILE.

See also ADDCHANNEL, DAQ/PRIVATE/SAVE, DAQ/PRIVATE/LOAD, OBJ2MFILE, DAQHELP, DAQDEVICE/DELETE, DAQ/PRIVATE/CLEAR.

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.

First, find any running data acquisition objects and stop them. This stops all running data acquisition objects from interfering with this example. This code is usually not necessary outside this example unless there are multiple data acquisition objects running.

if (~isempty(daqfind))
    stop(daqfind)
end

In this example, you will learn about saving analog input objects, and converting analog input objects to the equivalent MATLAB commands.

To get started, an analog input object with two channels is created.

ai = analoginput('winsound');
ch = addchannel(ai, [1 2], {'Left';'Right'});

The analog input object Tag, UserData, and SampleRate properties are assigned values.

ai.Tag = 'ai1';
ai.UserData = [1 4 2];
ai.SampleRate = 22050;

You can save an analog input object to a MAT-file with MATLAB's built-in SAVE command. Here we write the file 'fname.mat'.

save fname ai
delete(ai);

You can recreate the object when the MAT-file is loaded back into the MATLAB workspace with MATLAB's built-in LOAD command.

When loading an object into the MATLAB workspace, it is important to remember that all copies of an object in the workspace reference (point to) the same object in the data acquisition engine.

load fname
ai
Display Summary of Analog Input (AI) Object Using 'AK5370          '.
  Acquisition Parameters:  22050 samples per second on each channel.
                           8000 samples per trigger on each channel.
                           0.3628 sec. of data to be logged upon START.
                           Log data to 'Memory' on trigger.
      Trigger Parameters:  1 'Immediate' trigger(s) on START.
           Engine status:  Waiting for START.
                           0 samples acquired since starting.
                           0 samples available for GETDATA.
AI object contains channel(s):
   Index:  ChannelName:  HwChannel:  InputRange:  SensorRange:  UnitsRange:  Units:   
   1       'Left'        1           [-1 1]       [-1 1]        [-1 1]       'Volts'  
   2       'Right'       2           [-1 1]       [-1 1]        [-1 1]       'Volts'  

There are two variables, ai1 and ai, in the MATLAB workspace. However, there is only one object in the data acquisition engine that both ai and ai1 point to. Therefore, if you modify a property of ai1, the same property is modified in the variable ai.

ai1 = ai;          % referencing the same object

whos ai1 ai
  Name      Size            Bytes  Class          Attributes

  ai        1x1              1328  analoginput              
  ai1       1x1              1328  analoginput              

Originally, ai's UserData property is defined as follows:

  >> ai.UserData;

Now, let's modify ai1's UserData property to a new array.

  >> ai1.UserData = [3 5 1];

Now, let's check ai's UserData property value.

orig_ai = ai.UserData     % Original ai
ai1.UserData = [3 5 1];   % Modified ail
modif_ai = ai.UserData    % Verify ai
orig_ai =
     1     4     2
modif_ai =
     3     5     1

When loading an analog input object, MATLAB will first determine if the object already exists in the data acquisition engine. If it does exist, the object being loaded simply points to the object in the data acquisition engine.

However, if any properties are different between the object being loaded and the object that already exists in the engine, all properties will be modified to correspond to the object that is being loaded.

save fname ai      % let's save the analog input object

Let's assign the original values for the analog input Tag, UserData, and SampleRate properties to a new variable.

original_Tag = ai.Tag;
original_UserData = ai.UserData;
original_SampleRate = ai.SampleRate;

Now let's assign new values to these properties.

ai.Tag = 'myai1';
ai.UserData = [1 4 2];
ai.SampleRate = 44100;

When the file fname.mat is loaded, MATLAB determines that the analog input object being loaded already exists in the data acquisition engine. MATLAB also determines that three property values differ between the object being loaded and the object that exists in the data acquisition engine. These three properties are modified to correspond to the object that is being loaded.

load fname
Warning: The properties of an existing Data Acquisition object have been updated
to match the properties of an object being loaded.

Let's compare the Tag properties.

ai.Tag
original_Tag
ans =
ai1
original_Tag =
ai1

Let's compare the UserData properties.

ai.UserData
original_UserData
ans =
     3     5     1
original_UserData =
     3     5     1

Let's compare the SampleRate properties.

ai.SampleRate
original_SampleRate
ans =
       22050
original_SampleRate =
       22050

Similarly, channels will be added or deleted from an object that exists in the MATLAB workspace if the loaded object references the same object in the data acquisition engine and has a different number of channels.

save fname ai

Now, let's delete the second channel from analog input ai. Note that the object, ai, now only has one channel.

delete(ai.Channel(2));
ai.Channel  % verify one channel present
   Index:  ChannelName:  HwChannel:  InputRange:  SensorRange:  UnitsRange:  Units:   
   1       'Mono'        1           [-1 1]       [-1 1]        [-1 1]       'Volts'  

However, after loading fname.mat, ai will have the original channel setup since this was the channel configuration saved to fname.mat.

load fname
ai.Channel  % verify original channel setup
Warning: The properties of an existing Data Acquisition object have been updated
to match the properties of an object being loaded.
Warning: An existing Data Acquisition object has had channels added to match
the properties of an object being loaded.
   Index:  ChannelName:  HwChannel:  InputRange:  SensorRange:  UnitsRange:  Units:   
   1       'Left'        1           [-1 1]       [-1 1]        [-1 1]       'Volts'  
   2       'Right'       2           [-1 1]       [-1 1]        [-1 1]       'Volts'  

In summary, objects that exist in the MATLAB workspace can be modified by an object that is being loaded if:

  • The two objects point to the same object in the data acquisition engine.

  • The two objects have different property values.

Property values of the object that is being loaded have precedence over the property values of the objects that currently exist in the MATLAB workspace. Therefore, the object that is being loaded may change the properties of objects that exist in the MATLAB workspace.

delete fname.mat   % removing the saved file

You can convert an analog input object to MATLAB code using the OBJ2MFILE command. By default, the resulting MATLAB file will use the 'set' syntax for setting property values.

A MAT-file with the same name as the MATLAB file will also be created if:

  • You assign a value to the UserData property.

  • Any of the callback properties are defined as a cell array.

The MAT-file will contain the data stored by the UserData property as well as the cell array stored by the callback property.

obj2mfile(ai, 'fnameai');  % convert the ai object to MATLAB code
Created: D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fnameai.m
Created: fnameai.mat

The resulting MATLAB file appears below. Note the first line following the on-line help indicates the time the MATLAB file was created.

type fnameai.m
function out = fnameai
%FNAMEAI MATLAB Code for creating an Analog Input object.
%    
%    This is the machine generated representation of an Analog Input object.
%    This MATLAB file, FNAMEAI.M, was generated from the OBJ2MFILE function.
%    A MAT-file is created if the object's UserData property is not empty or
%    if any of the *Callback properties are set to a cell array.  The MAT-file 
%    will have the same name as the MATLAB file but with a .MAT extension.  To 
%    recreate this Analog Input object, type the name of the MATLAB file, FNAMEAI, 
%    at the MATLAB command prompt.
%    
%    The MATLAB file, FNAMEAI.M and its associated MAT-file, FNAMEAI.MAT (if it 
%    exists) must be on your MATLAB PATH.  For additional information
%    on setting your MATLAB PATH, type 'help addpath' at the MATLAB 
%    command prompt.
%    
%    Example: 
%       ai = fnameai;
%    
%    See also ANALOGINPUT, ANALOGOUTPUT, DIGITALIO, ADDCHANNEL, ADDLINE, 
%    DAQHELP, PROPINFO.
%    

%    Creation time: 27-Jul-2006 13:05:57

% Load the MAT-file which contains UserData and *Callback property values.
load fnameai

% Set the Analog Input non-default properties.
% Create the Analog Input object - object 1.
ai = analoginput('winsound', '0');

set(ai, 'SampleRate', 22050);
set(ai, 'Tag', 'ai1');
set(ai, 'UserData', userdata1);

% Create the Channel array.
chan = addchannel(ai, 1:2);

%Set Channel 1's non-default properties.
set(chan(1), 'ChannelName', 'Left');

%Set Channel 2's non-default properties.
set(chan(2), 'ChannelName', 'Right');


if nargout > 0 
   out = [ ai ];
end

The object is recreated by typing the name of the MATLAB file at the MATLAB command prompt.

delete(ai);
ai = fnameai
Display Summary of Analog Input (AI) Object Using 'AK5370          '.
  Acquisition Parameters:  22050 samples per second on each channel.
                           8000 samples per trigger on each channel.
                           0.3628 sec. of data to be logged upon START.
                           Log data to 'Memory' on trigger.
      Trigger Parameters:  1 'Immediate' trigger(s) on START.
           Engine status:  Waiting for START.
                           0 samples acquired since starting.
                           0 samples available for GETDATA.
AI object contains channel(s):
   Index:  ChannelName:  HwChannel:  InputRange:  SensorRange:  UnitsRange:  Units:   
   1       'Left'        1           [-1 1]       [-1 1]        [-1 1]       'Volts'  
   2       'Right'       2           [-1 1]       [-1 1]        [-1 1]       'Volts'  

You can pass a third input argument to OBJ2MFILE which specifies the syntax used in the MATLAB file. The possible values are 'set', 'dot', and 'named'. The default value is 'set'.

delete fnameai.m
delete fnameai.mat
obj2mfile(ai, 'fname', 'set');  % SET argument used
type fname.m
Created: D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fname.m
Created: fname.mat

function out = fname
%FNAME MATLAB Code for creating an Analog Input object.
%    
%    This is the machine generated representation of an Analog Input object.
%    This MATLAB file, FNAME.M, was generated from the OBJ2MFILE function.
%    A MAT-file is created if the object's UserData property is not empty or
%    if any of the *Callback properties are set to a cell array.  The MAT-file 
%    will have the same name as the MATLAB file but with a .MAT extension.  To 
%    recreate this Analog Input object, type the name of the MATLAB file, FNAME, 
%    at the MATLAB command prompt.
%    
%    The MATLAB file, FNAME.M and its associated MAT-file, FNAME.MAT (if it 
%    exists) must be on your MATLAB PATH.  For additional information
%    on setting your MATLAB PATH, type 'help addpath' at the MATLAB 
%    command prompt.
%    
%    Example: 
%       ai = fname;
%    
%    See also ANALOGINPUT, ANALOGOUTPUT, DIGITALIO, ADDCHANNEL, ADDLINE, 
%    DAQHELP, PROPINFO.
%    

%    Creation time: 27-Jul-2006 13:05:58

% Load the MAT-file which contains UserData and *Callback property values.
load fname

% Set the Analog Input non-default properties.
% Create the Analog Input object - object 1.
ai = analoginput('winsound', '0');

set(ai, 'SampleRate', 22050);
set(ai, 'Tag', 'ai1');
set(ai, 'UserData', userdata1);

% Create the Channel array.
chan = addchannel(ai, 1:2);

%Set Channel 1's non-default properties.
set(chan(1), 'ChannelName', 'Left');

%Set Channel 2's non-default properties.
set(chan(2), 'ChannelName', 'Right');


if nargout > 0 
   out = [ ai ];
end

Using the 'dot' notation:

delete fname.m
delete fname.mat
obj2mfile(ai, 'fname', 'dot');  % DOT argument used
type fname.m
Created: D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fname.m
Created: fname.mat

function out = fname
%FNAME MATLAB Code for creating an Analog Input object.
%    
%    This is the machine generated representation of an Analog Input object.
%    This MATLAB file, FNAME.M, was generated from the OBJ2MFILE function.
%    A MAT-file is created if the object's UserData property is not empty or
%    if any of the *Callback properties are set to a cell array.  The MAT-file 
%    will have the same name as the MATLAB file but with a .MAT extension.  To 
%    recreate this Analog Input object, type the name of the MATLAB file, FNAME, 
%    at the MATLAB command prompt.
%    
%    The MATLAB file, FNAME.M and its associated MAT-file, FNAME.MAT (if it 
%    exists) must be on your MATLAB PATH.  For additional information
%    on setting your MATLAB PATH, type 'help addpath' at the MATLAB 
%    command prompt.
%    
%    Example: 
%       ai = fname;
%    
%    See also ANALOGINPUT, ANALOGOUTPUT, DIGITALIO, ADDCHANNEL, ADDLINE, 
%    DAQHELP, PROPINFO.
%    

%    Creation time: 27-Jul-2006 13:05:58

% Load the MAT-file which contains UserData and *Callback property values.
load fname

% Set the Analog Input non-default properties.
% Create the Analog Input object - object 1.
ai = analoginput('winsound', '0');

ai.SampleRate = 22050;
ai.Tag = 'ai1';
ai.UserData = userdata1;

% Create the Channel array.
chan = addchannel(ai, 1:2);

%Set Channel 1's non-default properties.
chan(1).ChannelName = 'Left';

%Set Channel 2's non-default properties.
chan(2).ChannelName = 'Right';


if nargout > 0 
   out = [ ai ];
end

You can use the 'named' syntax only if the channel's ChannelName property is defined. Otherwise, an error will occur.

delete fname.m
delete fname.mat
obj2mfile(ai, 'fname', 'named');  % NAMED argument used
type fname.m
Created: D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fname.m
Created: fname.mat

function out = fname
%FNAME MATLAB Code for creating an Analog Input object.
%    
%    This is the machine generated representation of an Analog Input object.
%    This MATLAB file, FNAME.M, was generated from the OBJ2MFILE function.
%    A MAT-file is created if the object's UserData property is not empty or
%    if any of the *Callback properties are set to a cell array.  The MAT-file 
%    will have the same name as the MATLAB file but with a .MAT extension.  To 
%    recreate this Analog Input object, type the name of the MATLAB file, FNAME, 
%    at the MATLAB command prompt.
%    
%    The MATLAB file, FNAME.M and its associated MAT-file, FNAME.MAT (if it 
%    exists) must be on your MATLAB PATH.  For additional information
%    on setting your MATLAB PATH, type 'help addpath' at the MATLAB 
%    command prompt.
%    
%    Example: 
%       ai = fname;
%    
%    See also ANALOGINPUT, ANALOGOUTPUT, DIGITALIO, ADDCHANNEL, ADDLINE, 
%    DAQHELP, PROPINFO.
%    

%    Creation time: 27-Jul-2006 13:05:58

% Load the MAT-file which contains UserData and *Callback property values.
load fname

% Set the Analog Input non-default properties.
% Create the Analog Input object - object 1.
ai = analoginput('winsound', '0');

ai.SampleRate = 22050;
ai.Tag = 'ai1';
ai.UserData = userdata1;

% Create the Channel array.
chan = addchannel(ai, 1:2, {'Left' 'Right'});




if nargout > 0 
   out = [ ai ];
end

By default, the resulting MATLAB file will contain the commands for setting those properties which are not set to their default values. If you would like the MATLAB file to contain commands for each property, you can pass a fourth input argument, 'all', to OBJ2MFILE.

delete fname.m
delete fname.mat
obj2mfile(ai, 'fname', 'dot', 'all');
type fname.m
Created: D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fname.m
Created: fname.mat

function out = fname
%FNAME MATLAB Code for creating an Analog Input object.
%    
%    This is the machine generated representation of an Analog Input object.
%    This MATLAB file, FNAME.M, was generated from the OBJ2MFILE function.
%    A MAT-file is created if the object's UserData property is not empty or
%    if any of the *Callback properties are set to a cell array.  The MAT-file 
%    will have the same name as the MATLAB file but with a .MAT extension.  To 
%    recreate this Analog Input object, type the name of the MATLAB file, FNAME, 
%    at the MATLAB command prompt.
%    
%    The MATLAB file, FNAME.M and its associated MAT-file, FNAME.MAT (if it 
%    exists) must be on your MATLAB PATH.  For additional information
%    on setting your MATLAB PATH, type 'help addpath' at the MATLAB 
%    command prompt.
%    
%    Example: 
%       ai = fname;
%    
%    See also ANALOGINPUT, ANALOGOUTPUT, DIGITALIO, ADDCHANNEL, ADDLINE, 
%    DAQHELP, PROPINFO.
%    

%    Creation time: 27-Jul-2006 13:05:58

% Load the MAT-file which contains UserData and *Callback property values.
load fname

% Set the Analog Input properties.
% Create the Analog Input object - object 1.
ai = analoginput('winsound', '0');

ai.BitsPerSample = 16;
ai.BufferingMode = 'Auto';
ai.ChannelSkewMode = 'None';
ai.ClockSource = 'Internal';
ai.DataMissedFcn = datamissedfcn1;
ai.InputOverRangeFcn = [];
ai.InputType = 'AC-Coupled';
ai.LogFileName = 'logfile.daq';
ai.LoggingMode = 'Memory';
ai.LogToDiskMode = 'Overwrite';
ai.ManualTriggerHwOn = 'Start';
ai.Name = 'winsound0-AI';
ai.RuntimeErrorFcn = runtimeerrorfcn1;
ai.SampleRate = 22050;
ai.SamplesAcquiredFcn = [];
ai.SamplesAcquiredFcnCount = 1024;
ai.SamplesPerTrigger = 8000;
ai.StandardSampleRates = 'On';
ai.StartFcn = [];
ai.StopFcn = [];
ai.Tag = 'ai1';
ai.Timeout = 1;
ai.TimerFcn = [];
ai.TimerPeriod = 0.1;
ai.TriggerChannel = [];
ai.TriggerCondition = 'None';
ai.TriggerConditionValue = 0;
ai.TriggerDelay = 0;
ai.TriggerDelayUnits = 'Seconds';
ai.TriggerFcn = [];
ai.TriggerRepeat = 0;
ai.TriggerType = 'Immediate';
ai.UserData = userdata1;

% Create the Channel array.
chan = addchannel(ai, 1:2);

%Set Channel 1's properties.
chan(1).ChannelName = 'Left';
chan(1).InputRange = [-1 1];
chan(1).SensorRange = [-1 1];
chan(1).Units = 'Volts';
chan(1).UnitsRange = [-1 1];

%Set Channel 2's properties.
chan(2).ChannelName = 'Right';
chan(2).InputRange = [-1 1];
chan(2).SensorRange = [-1 1];
chan(2).Units = 'Volts';
chan(2).UnitsRange = [-1 1];


if nargout > 0 
   out = [ ai ];
end

It is also possible to convert only a channel to MATLAB code rather than the entire analog input object.

For example, you can use the following commands to create a MATLAB file that includes only those channel properties not set to their default values using the default 'set' notation.

delete fname.m
delete fname.mat
chan = ai.Channel;
obj2mfile(chan, 'fname');
type fname.m
Created: D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fname.m

function out = fname(parent)
%FNAME MATLAB Code for creating an array of Channel objects.
%    
%    FNAME(PARENT) appends the array of Channel objects defined
%    in FNAME to the Data Acquisition object, PARENT.
%    
%    This is the machine generated representation of a Channel object.
%    This MATLAB file was generated from the OBJ2MFILE function.  To recreate 
%    the Channel object, type the name of the MATLAB file, FNAME, at the MATLAB 
%    command prompt and pass a PARENT object as input.  The PARENT object
%    should be of class 'analoginput'.
%    
%    The MATLAB file, FNAME, must be on your MATLAB PATH.  For additional
%    information on setting your MATLAB PATH, type 'help addpath' at the
%    MATLAB command prompt.
%    
%    Example:
%       parent = analoginput('winsound',0);
%       chan = fname(parent);
%    
%    See also ANALOGINPUT, ANALOGOUTPUT, DIGITALIO, ADDCHANNEL, ADDLINE
%    DAQHELP, PROPINFO.
%    

%    Creation time: 27-Jul-2006 13:05:58

% Error if input is incorrect.
if (nargin ~= 1) | ~isa(parent, 'daqdevice')
   error(['One device object is expected as input.  ',...
         'See the on-line help for an example.']);
end

% Create the Channel array.
chan = addchannel(parent, 1:2);

%Set Channel 1's non-default properties.
set(chan(1), 'ChannelName', 'Left');

%Set Channel 2's non-default properties.
set(chan(2), 'ChannelName', 'Right');

if nargout > 0, out = chan; end;

If you wanted to create a MATLAB file that defines all properties using the 'named' syntax, the following command is used.

delete fname.m
obj2mfile(chan, 'fnamechan', 'named', 'all');
type fnamechan.m
Created: D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fnamechan.m

function out = fnamechan(parent)
%FNAMECHAN MATLAB Code for creating an array of Channel objects.
%    
%    FNAMECHAN(PARENT) appends the array of Channel objects defined
%    in FNAMECHAN to the Data Acquisition object, PARENT.
%    
%    This is the machine generated representation of a Channel object.
%    This MATLAB file was generated from the OBJ2MFILE function.  To recreate 
%    the Channel object, type the name of the MATLAB file, FNAMECHAN, at the MATLAB 
%    command prompt and pass a PARENT object as input.  The PARENT object
%    should be of class 'analoginput'.
%    
%    The MATLAB file, FNAMECHAN, must be on your MATLAB PATH.  For additional
%    information on setting your MATLAB PATH, type 'help addpath' at the
%    MATLAB command prompt.
%    
%    Example:
%       parent = analoginput('winsound',0);
%       chan = fnamechan(parent);
%    
%    See also ANALOGINPUT, ANALOGOUTPUT, DIGITALIO, ADDCHANNEL, ADDLINE
%    DAQHELP, PROPINFO.
%    

%    Creation time: 27-Jul-2006 13:05:59

% Error if input is incorrect.
if (nargin ~= 1) | ~isa(parent, 'daqdevice')
   error(['One device object is expected as input.  ',...
         'See the on-line help for an example.']);
end

% Create the Channel array.
chan = addchannel(parent, 1:2, {'Left' 'Right'});

%Set Channel 1's properties.
parent.Left.InputRange = [-1 1];
parent.Left.SensorRange = [-1 1];
parent.Left.Units = 'Volts';
parent.Left.UnitsRange = [-1 1];

%Set Channel 2's properties.
parent.Right.InputRange = [-1 1];
parent.Right.SensorRange = [-1 1];
parent.Right.Units = 'Volts';
parent.Right.UnitsRange = [-1 1];

if nargout > 0, out = chan; end;

The channels are recreated by typing the name of the MATLAB file with an analog input object as an input argument at the MATLAB command prompt.

delete(ai);
ai = analoginput('winsound');
which fnamechan;
chan = fnamechan(ai)   % recreate the channel
D:\Work\R2007a\matlab\toolbox\daq\daqdemos\fnamechan.m
   Index:  ChannelName:  HwChannel:  InputRange:  SensorRange:  UnitsRange:  Units:   
   1       'Left'        1           [-1 1]       [-1 1]        [-1 1]       'Volts'  
   2       'Right'       2           [-1 1]       [-1 1]        [-1 1]       'Volts'  

Lastly, delete the analog input object and remove saved data acquisition files.

delete(ai)
delete fnamechan.m
Was this topic helpful?