matlab.io.saveVariablesToScript

Save workspace variables to MATLAB script

Syntax

  • matlab.io.saveVariablesToScript(filename) example
  • matlab.io.saveVariablesToScript(filename,varnames) example
  • matlab.io.saveVariablesToScript(filename,Name,Value) example
  • [r1,r2] = matlab.io.saveVariablesToScript(filename)

Description

example

matlab.io.saveVariablesToScript(filename) saves variables in the current workspace to a MATLAB® script named filename.m. The filename can include the .m suffix. If you do not include it, the function adds it when it creates the file.

Variables that MATLAB cannot generate code for are saved to a MAT-file named filename.mat.

If a file with the same name already exists, it is overwritten.

example

matlab.io.saveVariablesToScript(filename,varnames) saves only workspace variables specified by varnames to the MATLAB script.

example

matlab.io.saveVariablesToScript(filename,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

[r1,r2] = matlab.io.saveVariablesToScript(filename) additionally returns two cell arrays:

  • r1 for variables that were saved to the MATLAB script

  • r2 for variables that were saved to a MAT-file

Examples

expand all

Save Workspace Variables to MATLAB Script

Save variables from a workspace to a MATLAB script, test.m.

matlab.io.saveVariablesToScript('test.m')

Save Specific Workspace Variables to MATLAB Script

Create and save variable myVar from a workspace to a MATLAB script, test.m.

myVar = 55.3;
matlab.io.saveVariablesToScript('test.m','myVar')

Append Specific Variables to Existing MATLAB Script

Create two variables, a and b, and save them to an existing MATLAB script abfile.m.

a = 72.3;
b = pi;
matlab.io.saveVariablesToScript('abfile.m',{'a','b'},...
'SaveMode','append')

Update Specific Variables in Existing MATLAB Script

Update and save two variables, y and z, to an existing MATLAB script yzfile.m.

y = 15.7;
z = 3 * pi;
matlab.io.saveVariablesToScript('yzfile.m',{'y','z'},...
'SaveMode','update')

Specify MATLAB Script Configuration for Saving Variable

Update and save variable resistance to an existing MATLAB script designData.m while specifying the configuration of the script file.

resistance = [10 20.5 11 13.7 15.1 7.7];
matlab.io.saveVariablesToScript('designData.m','resistance',...
'SaveMode','Update','MaximumArraySize',5,...
'MaximumNestingLevel',5,'MaximumTextWidth',30)

Specify 2-D Slice for Saving 3-D Array in MATLAB Script

Specify a 2-D slice for the output of the 3-D array my3Dtable, such that the 2-D slice expands along the first and third dimensions. Save the 2-D slice in the MATLAB script sliceData.m.

level1 = [1 2; 3 4];
level2 = [5 6; 7 8];
my3Dtable( :, :, 1) = level1;
my3Dtable( :, :, 2) = level2;
matlab.io.saveVariablesToScript('sliceData.m','MultidimensionalFormat',[1,3])

The resulting MATLAB code is similar to the following:

level1 = ...
	[1 2;
	3 4];

level2 = ...
	[5 6;
	7 8];
my3Dtable = zeros(2, 2, 2);
my3Dtable(:,1,:) = ...
	[1 5;
	3 7];

my3Dtable(:,2,:) = ...
	[2 6;
	4 8];

Save Variables Matching a Regular Expression

Save variables that match the expression autoL* to a MATLAB script autoVariables.m.

matlab.io.saveVariablesToScript('autoVariables.m','RegExp','autoL*')

Save Variables to Version 7.3 MATLAB Script

Create two variables, p and q, and save them to a version 7.3 MATLAB script version73.m.

p = 49;
q = 35.5;
matlab.io.saveVariablesToScript('version73.m','p','q',...
'MATFileVersion','v7.3')

Return Variables Saved to MATLAB Script

Save variables that were saved to a MATLAB script to the variable r1, and those that were saved to a MAT-file to the variable r2.

[r1,r2] = matlab.io.saveVariablesToScript('mydata.m')
r1 = 

    'level1'
    'level2'
    'level3'
    'my3Dtable'


r2 = 

   Empty cell array: 0-by-1

Input Arguments

expand all

filename — Name of MATLAB script for saving variablesfilename | variable

Name of MATLAB script for saving variables, specified as a string giving a file name or a variable containing the file name.

Example: matlab.io.saveVariablesToScript('myVariables.m')

varnames — Name of variables to savestring | cell array

Name of variables to save, specified as a string or a cell array.

Example: {'X','Y','Z'}

Data Types: char | cell

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'MaximumArraySize',500,'MATFileVersion','v4' specifies that the maximum number of array elements to save is 500 using MATLAB version 4 syntax.

'MATFileVersion' — MATLAB version whose syntax to use'v7.3' (default) | 'v4' | 'v6' | 'v7'

MATLAB version whose syntax to use for saving MAT-files, specified as the comma-separated pair consisting of 'MATFileVersion' and one of the following version numbers:

  • 'v4'

  • 'v6'

  • 'v7'

  • 'v7.3'

Example: 'MATFileVersion','v6'

Data Types: char

'MaximumArraySize' — Maximum array elements to save1000 (default) | integer

Maximum array elements to save, specified as the comma-separated pair consisting of 'MaximumArraySize' and an integer in the range of 1 to 10,000.

Example: 'MaximumArraySize',1050

'MaximumNestingLevel' — Maximum number of object levels or array hierarchies to save20 (default) | integer

Maximum number of object levels or array hierarchies to save, specified as the comma-separated pair consisting of 'MaximumNestingLevel' and an integer in the range of 1 to 200.

Example: 'MaximumNestingLevel',67

'MaximumTextWidth' — Text wrap width during save76 (default) | integer

Text wrap width during save, specified as the comma-separated pair consisting of 'MaximumTextWidth' and an integer in the range of 32 to 256.

Example: 'MaximumTextWidth',82

'MultidimensionalFormat' — Dimensions of 2-D slices that represent n-D arrays of char, logic, or numeric data'rowvector' (default) | integer cell array

Dimensions of 2-D slices that represent n-D arrays of char, logic, or numeric data, specified as the comma-separated pair consisting of 'MultidimensionalFormat' and one of these values:

  • 'rowvector' — Save multidimensional variables as a single row vector.

  • integer cell array — Save a 2-D slice of multidimensional variables, where the dimensions satisfy all the following criteria:

    • Two positive integers represent dimensions.

    • The two integers are less than or equal to the dimensions of the n-D array.

    • The second integer is greater than the first.

Example: 'MultidimensionalFormat',[1,3]

'RegExp' — Regular expression for matchingstring

Regular expression for matching, specified as the comma-separated pair consisting of 'RegExp' and one or more expressions given as a string.

Example: 'RegExp','level*'

Data Types: char

'SaveMode' — Mode to save MATLAB script'create' (default) | 'update' | 'append'

Mode to save MATLAB script, specified as the comma-separated pair consisting of SaveMode and one of these values:

  • 'create' — Save variables to a new MATLAB script.

  • 'update' — Only update variables that are already present in a MATLAB script.

  • 'append' — Update variables that are already present in a MATLAB script and append new variables to the end of the script.

Example: 'SaveMode','Update'

Output Arguments

expand all

r1 — Variables that were saved to a MATLAB scriptcell array

Variables that were saved to a MATLAB script, returned as a cell array of variable names.

r2 — Variables that were saved to a MAT-filecell array

Variables that were saved to a MAT-file, returned as a cell array of variable names.

Limitations

  • matlab.io.saveVariablesToScript does not save the following variables to a MATLAB script or a MAT-file.

    • Java objects

    • .NET objects

  • matlab.io.saveVariablesToScript saves the following variables only to a MAT-file.

    • MATLAB objects

    • Function handles

    • Anonymous functions

Was this topic helpful?