matlab.io.saveVariablesToScript - Save workspace variables to MATLAB script - MATLAB (original) (raw)
Save workspace variables to MATLAB script
Syntax
Description
matlab.io.saveVariablesToScript([filename](#bt6f6kw-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 are too large or that MATLAB cannot generate code for are saved to a MAT-file namedfilename.mat
.
If a file with the same name already exists, it is overwritten.
matlab.io.saveVariablesToScript([filename](#bt6f6kw-filename),[varnames](#bt6f6kw-varnames))
saves only workspace variables specified by varnames
to the MATLAB script.
matlab.io.saveVariablesToScript([filename](#bt6f6kw-filename),[Name,Value](#namevaluepairarguments))
uses additional options specified by one or more Name,Value
pair arguments.
[[r1](#bt6f6kw-r1),[r2](#bt6f6kw-r2)] = matlab.io.saveVariablesToScript([filename](#bt6f6kw-filename))
additionally returns two cell arrays:
r1
for variables that were saved to the MATLAB scriptr2
for variables that were saved to a MAT-file
Examples
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 test.m
.
a = 72.3; b = pi; c = "string1"; matlab.io.saveVariablesToScript('test.m',{'a','b','c'},... 'SaveMode','append')
Update and save two variables, a
and b
, to an existing MATLAB script test.m
.
a = 15.7; b = 3 * pi; matlab.io.saveVariablesToScript('test.m',{'a','b'},... 'SaveMode','update')
Save variable resistance
to an existing MATLAB script test.m
while specifying the configuration of the script file.
resistance = [10 20.5 11 13.7]; matlab.io.saveVariablesToScript('test.m','resistance',... 'SaveMode','append','MaximumArraySize',5,... 'MaximumNestingLevel',5,'MaximumTextWidth',35)
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 level*
to a new MATLAB script levelVariables.m
.
matlab.io.saveVariablesToScript('levelVariables.m','RegExp','level*')
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 =
5×1 cell array
{'level1' }
{'level2' }
{'my3Dtable'}
{'p' }
{'q' }
r2 =
0×1 empty cell array
Input Arguments
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 Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: 'MaximumArraySize',500,'MATFileVersion','v4'
specifies that the maximum number of array elements to save is 500
using MATLAB version 4 syntax.
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
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
- Missing values in string arrays
If you have Simulink®, you can use matlab.io.saveVariablesToScript
to save the variables that your models use. If a Simulink data object in the workspace contains a large array, the function saves only the numeric value to a MAT file and writes the rest of the data object to a MATLAB file.
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:
- Permanently store variables in a data dictionary. A data dictionary also provides more tools for managing variables. See Determine Where to Store Variables and Objects for Simulink Models (Simulink).
- Save variables in a MAT-file by using the save function.
Version History
Introduced in R2014a
When a data object in the workspace contains a large array, the function saves only the numeric value to a MAT file and writes the rest of the data object to a MATLAB file. A large array is an array with more elements than the number of elements specified by the MaximumArraySize
argument which, by default, is 1000 array elements.