文档

节省

将工作区变量保存到文件

句法

保存(文件名)
节省(filename,variables)
保存(文件名,变量,FMT)
节省(filename,variables,version)
保存(文件名,变量,版本,“ - nocompression'))
节省(filename,variables,'-append')
保存(文件名,变量,' - append',' - nocompression')
保存文件名

描述

例子

节省(filename将当前工作空间中的所有变量保存在MATLAB中®格式ted binary file (MAT-file) calledfilename。如果filenameexists,节省覆盖文件。

例子

节省(filename,,,,variables仅保存由结构数组的变量或字段variables

例子

节省(filename,,,,variables,,,,FMT保存以文件格式保存FMT。这variables参数是可选的。如果您不指定variables, 这节省功能将所有变量保存在工作区中。

例子

节省(filename,,,,variables,,,,版本节省s to the MAT-file version specified by版本。这variables参数是可选的。

例子

节省(filename,,,,variables,,,,版本,,,,' - 调整')将变量保存到垫子文件中而无需压缩。这' - 调整'flag only supports MAT-file Version 7.3. Therefore, you must specify版本作为'-v7.3'。这variables参数是可选的。

例子

节省(filename,,,,variables,,,,'-附加')将新变量添加到现有文件中。如果垫子中已经存在变量,则节省用工作区中的值覆盖它。

对于ASCII文件,'-附加'一个dds data to the end of the file.

要附加到6个垫子文件中,您还必须包括'-v6'作为输入参数。

例子

节省(filename,,,,variables,' - append',' - nocompression')一个dds new variables to an existing file without compression. The existing file must be a MAT-file Version 7.3.

例子

节省filename是语法的命令表格。命令表需要更少的特殊字符。您无需键入括号或将输入包装在单个或双引号中。带有空格而不是逗号的单独输入。

例如,保存一个名称的文件test.mat, 这se statements are equivalent:

节省test.mat % command form save('test.mat') % function form

您可以包含以前语法中描述的任何输入。例如,保存命名的变量X

保存test.mat x%命令表格保存('test.mat','x')%函数表格

当任何输入时,请勿使用命令表格filename,是变量。

例子

全部收缩

s一个ve all variables from the workspace in a binary MAT-file,test.mat。如果filename是变量,使用函数语法。

文件名='test.mat';保存(文件名)

否则,您也可以使用命令语法。

节省test.mat

从工作区中删除变量,然后用加载功能。

清除加载('test.mat'

创建并保存两个变量,p一个nd,到称为的文件pqfile.mat

p = rand(1,10);Q =一个(10);节省('pqfile.mat',,,,'P',,,,'Q'

matlab®将变量保存到文件中,pqfile.mat,在当前文件夹中。

您还可以使用命令语法保存变量,p一个nd

节省pqfile.matp

创建两个变量,将它们保存到ASCII文件,然后查看文件的内容。

p = rand(1,10);Q =一个(10);节省('pqfile.txt',,,,'P',,,,'Q',,,,'-ascii') 类型('pqfile.txt'

类型功能显示文件的内容。

一个lternatively, use command syntax for the节省手术。

节省p问file.txtp-ascii

创建一个结构s1,其中包含三个字段,一个,,,,b,,,,一个ndC

s1.a = 12.7; s1.b = {'abc',,,,[4 5; 6 7]}; s1.c ='你好!';

保存结构字段s1作为individual variables in a file callednewsstruct.mat

节省('newstruct.mat',,,,' - 结构',,,,'S1');

Check the contents of the file using the谁是功能。

disp(“ newsstruct.mat的内容:”
newsstruct.mat的内容:
谁是('-文件',,,,'newstruct.mat'
名称大小字节类属性A 1x1 8 double B 1x2 262单元格1x6 12 char

创建两个变量,然后将它们保存到7.3版垫子文件中示例.mat

一个= rand(5); B = magic(10); save('example.mat',,,,'A',,,,'B',,,,'-v7.3'

您还可以将命令语法用于节省手术。

节省示例.mat一个b-v7.3

Create two variables and save them, without compression, to a Version7.3垫子叫做myfile.mat

一个= rand(5); B = magic(10); save('myfile.mat',,,,'A',,,,'B',,,,'-v7.3',,,,' - 调整'

一个lternatively, use the command syntax for the节省手术。

节省myfile.mat一个b-v7.3-nocompression

' - 调整'flag faciliates a faster save for those variables that are larger than2GB或不从压缩中受益的GB。

将两个变量保存到垫子文件中。然后,将第三个变量附加到同一文件。

p = rand(1,10);Q =一个(10);节省('test.mat',,,,'P',,,,'Q'

查看垫子文件的内容。

谁是('-文件',,,,'test.mat'
名称大小字节类属性p 1x10 80 double q 10x10 800 double double

创建一个新变量,,,,一个并将其附加到垫子。

一个= 50; save('test.mat',,,,'a',,,,'-附加'

查看垫子文件的内容。

谁是('-文件',,,,'test.mat'
名称大小字节类属性A 1x1 8 double P 1x10 80 double Q 10x10 800 double

这variable,一个,附加到test.mat,没有覆盖以前的变量,p一个nd

笔记

要附加到6个垫子文件,请两者指定'-v6'一个nd'-附加'。例如,保存变量一个到文件,test.mat, 称呼:

节省('test.mat',,,,'a',,,,'-v6',,,,'-附加'

将两个变量保存到垫子文件中。这n, append a third variable, without compression, to the same file.

Create two variables一个一个ndb并将它们保存到垫子文件中7.3。by default, the节省函数压缩变量一个一个ndb在将它们保存到之前myfile.mat

一个= rand(5); B = magic(10); save('myfile.mat',,,,'A',,,,'B',,,,'-v7.3'

查看垫子文件的内容。

谁是('-文件',,,,'myfile.mat'
名称大小字节类属性A 5x5 200 double B 10x10 800 double

创建一个新变量C一个nd append it, without compression, tomyfile.mat

C = 5;节省('myfile.mat',,,,'C',,,,'-附加',,,,' - 调整'

查看垫子文件的内容。

谁是('-文件',,,,'myfile.mat'
名称大小字节类属性A 5x5 200双B 10x10 800双C 1x1 8 double

输入参数

全部收缩

文件的名称,指定为字符向量或字符串。如果您不指定filename, 这节省函数保存到名称的文件matlab.mat

如果filename没有扩展名(即,没有延期,后期有文本),格式未指定,然后是matlab附加。mat。如果filenamedoes not include a full path, MATLAB saves to the current folder. You must have permission to write to the file.

使用命令表格节省,,,,it is unnecessary to enclose the input in single or double quotes. However, iffilename包含一个空间,您必须将参数包装在单引号中。例如,节省'filename withspace.mat'

例子:'myfile.mat'

数据类型:char|string

要保存的变量名称,,,,specified as one or more character vectors or strings. When using the command form of节省,您无需将输入包装在单引号或双引号中。variables可以是以下形式之一。

形式variables输入 要保存的变量
var1,...,varn s一个ve the listed variables, specified as individual character vectors or strings.
使用'*'wildcard to match patterns. For example,保存('filename.mat','a*')节省s all variables in the file that start with一个
'-regexp',expr1,...,exprn 仅保存名称匹配的变量regular expressions,指定为字符矢量或字符串。例如,保存('filename.mat',' - regexp','^mon','^tues')节省s only the variables in the file whose names begin with周一or星期二
' - struct',structname store the fields of the scalar structure specified bystructname作为文件中的单个变量。例如,节省('filename.mat','-struct','S')保存标量结构,s
' - struct',structname,field1,...,fieldn 将指定标量结构的指定字段存储为文件中的单个变量。例如,节省('filename.mat,'-struct','S','a','b')保存字段s。一个一个nds。b
' - struct',structname,' - regexp',expr1,...,exprn 仅存储名称匹配的字段regular expressions,指定为字符矢量或字符串。

数据类型:char|string

文件格式,,,,specified as one of the following. When using the command form of节省,您不需要将输入包装在单个或双引号中保存myfile.txt -ascii -tabs

的价值FMT 文件格式
'-mat'

binary MAT-file format.

'-ascii'

文本格式,具有8位精度。

'-ascii',' - tabs'

选项卡划分的文本格式,具有8位精度。

'-ascii',' - double'

文本格式,具有16位精度。

'-ascii',' - double',,,,'-tabs'

Tab-delimited text format with 16 digits of precision.

For MAT-files, data saved on one machine and loaded on another machine retains as much accuracy and range as the different machine floating-point formats allow.

使用一种文本格式将MATLAB数字值保存到文本文件。在这种情况下:

  • 每个变量必须是二维双倍的大批。

  • 这output includes only the real component of complex numbers.

  • MATLAB将每个变量的数据顺序写入文件。如果您打算使用加载要读取文件的功能,所有变量都必须具有相同数量的列。这加载function creates a single variable from the file.

如果you specify a text format and any variable is a two-dimensional character array, then MATLAB translates characters to their corresponding internal ASCII codes. For example,'abc'出现在文本文件中:

9.7000000e+001 9.8000000e+001 9.9000000e+001

数据类型:char|string

MAT-FILE版本,指定为以下内容之一。使用命令表格节省,您无需将输入包装在单引号或双引号中。

的价值版本 Loads in MATLAB Versions 金宝app支持的功能 Compression Maximum Size of Each Variable
'-v7.3' 7.3(R2006b)或更高版本

s一个ving and loading parts of variables, and all Version 7 features. Version 7.3 also supports saving variables without compression using the' - 调整'选项。

Yes (default) ≥ 2 GB on 64-bit computers
'-v7' 7.0 (R14) or later

Unicode®char一个Cter encoding, which enables file sharing between systems that use different default character encoding schemes, and all Version 6 features.

Yes 2^31 bytes per variable
'-v6' 5(R8)或更高版本

n维数组,单元格数组,结构数组,可变名称超过19个字符以及所有版本4功能。

No 2^31 bytes per variable
'-v4' 一个ll

二维双倍的,角色和稀疏阵列。

No 每个阵列的100,000,000个元素,每个变量2^31字节

如果任何数据项都需要指定版本不支持的功能,则MATLAB不会保存这些项目并发出警告。金宝app您不能比当前版本的MATLAB软件指定版本。

笔记

tha mat文件使用一个基于HDF5格式7.3版本t requires some overhead storage to describe the contents of the file. For cell arrays, structure arrays, or other containers that can store heterogeneous data types, Version 7.3 MAT-files are sometimes larger than Version 7 MAT-files.

要查看或设置Mat-Files的默认版本,请选择一个垫子文件保存格式option in the General Preferences.

提示

  • 要获得创建ASCII文件的更灵活性,请使用dlmwriteorfprintf

  • s一个ving graphics objects with the节省函数可能会导致一个大文件,因为该文件包含重新生成对象所需的所有信息。

  • 避免使用节省功能。使用节省figfunction instead. Using节省在R2014B或更晚的时间内保存一个数字,使MATLAB版本中的垫子无法访问。如果您使用节省为了保存图形,该功能显示警告消息。使用之前删除所有数字节省。Keep in mind that the figures might not be directly in your workspace. For example, they might be stored in a structure or in the workspace of a callback function.

Introduced before R2006a

Was this topic helpful?