Main Content

Saving and Loading Instrument Objects

Saving Instrument Objects to a File

You can save an instrument 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 instrument 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 or the dot notation.

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 instrument object is saved. The MAT-file has the same name as the file containing the instrument object code.

For example, suppose you create the GPIB object g, return instrument identification information to the variable out, and store out in the UserData property.

g = gpib('ni',0,1);
g.Tag = 'My GPIB object';
fopen(g)
cmd = '*IDN?';
fprintf(g,cmd)
out = fscanf(g);
g.UserData = out;

The following command saves g and the modified property values to the file mygpib.m. Because the UserData property is not empty, its value is automatically written to the MAT-file mygpib.mat.

obj2mfile(g,'mygpib.m');

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

Loading the Instrument Object

To load an instrument object that was saved as a file into the MATLAB workspace, type the name of the file at the command line. For example, to load g from the file mygpib.m,

g = mygpib

The display summary for g is shown below. Note that the read-only properties such as Status, BytesAvailable, ValuesReceived, and ValuesSent are restored to their default values.

GPIB Object Using NI Adaptor : GPIB0-1

Communication Address 
   BoardIndex:         0
   PrimaryAddress:     1
   SecondaryAddress:   0

Communication State 
   Status:             closed
   RecordStatus:       off

Read/Write State  
   TransferStatus:     idle
   BytesAvailable:     0
   ValuesReceived:     0
   ValuesSent:         0

When loading g into the workspace, the MAT-file mygpib.mat is automatically loaded and the UserData property value is restored.

g.UserData
ans =
TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.16 TDS2CM:CMV:v1.04

Saving Objects to a MAT-File

You can save an instrument object to a MAT-file just as you would any workspace variable — using the save command. For example, to save the GPIB object g and the variables cmd and out, defined in Saving Instrument Objects to a File, to the MAT-file mygpib1.mat,

save mygpib1 g cmd out

Read-only property values are not saved. Therefore, read-only properties use their default values when you load the instrument 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 Instrument Object

To load an instrument object that was saved to a MAT-file into the MATLAB workspace, use the load command. For example, to load g, cmd, and out from MAT-file mygpib1.mat,

load mygpib1

The display summary for g is shown below. Note that the read-only properties such as Status, BytesAvailable, ValuesReceived, and ValuesSent are restored to their default values.

GPIB Object Using NI Adaptor : GPIB0-1

Communication Address 
   BoardIndex:         0
   PrimaryAddress:     1
   SecondaryAddress:   0

Communication State 
   Status:             closed
   RecordStatus:       off

Read/Write State  
   TransferStatus:     idle
   BytesAvailable:     0
   ValuesReceived:     0
   ValuesSent:         0