Documentation Center

  • Trial Software
  • Product Updates

Save and Load Device Objects

Save Device Objects to a File

    Note:   For analog input objects, you can also save acquired data, hardware information, and so on to a log file. Refer to Log Information to Disk for more information.

You can save a device object to a file using the obj2mfile function. obj2mfile provides you with these options:

  • Save all property values, or save only those property values that differ from their default values.

    Read-only property values are not saved. Therefore, read-only properties use their default values when you load the device object into the MATLAB® workspace. To determine if a property is read-only, use the propinfo function or examine the property reference pages.

  • Save property values using the set syntax, the dot notation, or named referencing (if defined).

    If the UserData property is not empty, or if a callback property is set to a cell array of values or a function handle, then the data stored in these properties is written to a MAT-file when the device object is saved. The MAT-file has the same name as the file containing the device object code.

For example, suppose you create the analog input object ai for a sound card, add two channels to it, and configure several property values.

ai = analoginput('winsound');
addchannel(ai,1:2,{'Temp1';'Temp2'}); 
time = now;
set(ai,'SampleRate',11025,'TriggerRepeat',4)
set(ai,'TriggerFcn',{@mycallback,time})
start(ai)

The following command saves ai and the modified property values to the file myai.m. Because the TriggerFcn property is set to a cell array of values, its value is automatically written to the MAT-file myai.mat.

obj2mfile(ai,'myai.m');

Created: d:\v6\myfiles\myai.m
Created: d:\v6\myfiles\myai.mat

Use the type command to display myai.m at the command line.

Load the Device Object

To load a device object that was saved as a file into the MATLAB workspace, type the name of the file at the Command Window. For example, to load ai from the file myai.m:

ai = myai

Note that the read-only properties such as SamplesAcquired and SamplesAvailable are restored to their default values.

get(ai,{'SamplesAcquired','SamplesAvailable'})
ans = 
    [0]    [0]

When loading ai into the workspace, the MAT-file myai.mat is automatically loaded and the TriggerFcn property value is restored.

ai.TriggerFcn
ans = 
    [@mycallback]    [7.3071e+005]

Save Device Objects to a MAT-File

    Note:   For analog input objects, you can also save acquired data, hardware information, and so on to a log file. Refer to Log Information to Disk for more information.

You can save a device object to a MAT-file just as you would any workspace variable — using the save command. For example, to save the analog input object ai and the variable time defined in the preceding section to the MAT-file myai1.mat:

save myai1 ai time

Read-only property values are not saved. Therefore, read-only properties use their default values when you load the device object into the MATLAB workspace. To determine if a property is read-only, use the propinfo function or examine the property reference pages.

Loading the Device Object

To load a device object that was saved to a MAT-file into the MATLAB workspace, use the load command. For example, to load ai and time from MAT-file myai1.mat:

load myai1
Was this topic helpful?