Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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

collapse all

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

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

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

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

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 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')

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 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 that match the expression autoL* to a MATLAB script autoVariables.m.

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

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')

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

collapse all

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')

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.

collapse all

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

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

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

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

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]

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

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

collapse all

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

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

    • Python objects

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

    • MATLAB objects

    • Function handles

    • Anonymous functions

If you have Simulink®, you can use matlab.io.saveVariablesToScript to save the variables that your models use. However, if you save many variables, the generated MATLAB file can contain many lines of code and take a long time to execute. To avoid the long execution time, consider these alternatives:

Was this topic helpful?