Main Content

writematrix

向文件写一个矩阵

Description

例子

writematrix(一种writes homogeneous array一种逗号分隔的文本文件。文件名是阵列的Workspace变量名称,附加到扩展名。txt。如果writematrixcannot construct the file name from the array name, then it writes to the file矩阵。txt

每个变量的每个列一种becomes a column in the output file. Thewritematrixfunction overwrites any existing file.

例子

writematrix(一种filenamewrites to a file with the name and extension specified byfilename

writematrixdetermines the file format based on the specified extension. The extension must be one of the following:

  • 。txt.DAT.那or。csvfor delimited text files

  • .xls..xls.m那or.xls.xfor Excel®电子表格文件

  • .xls.bfor Excel spreadsheet files supported on systems with Excel for Windows®

例子

writematrix(___名称,价值writes an array to a file with additional options specified by one or more名称,价值pair arguments and can include any of the input arguments in previous syntaxes.

Examples

collapse all

创建一个矩阵,将其写入逗号分隔的文本文件,然后用不同的分隔符字符将矩阵写入另一个文本文件。

Create a matrix in the workspace.

m =魔术(5)
M =5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

Write the matrix to a comma delimited text file and display the file contents. Thewritematrixfunction outputs a text file namedm.txt.

writematrix(m)类型'm.txt.'
17,24,1,8,15 23,5,7,14,16 4,6,13,20,22 10,12,19,21,3 11,18,25,2,9

要用不同的分隔符字符写入与文本文件相同的矩阵,请使用'delimiter'的名字-value pair.

Writematrix(M,'m_tab.txt''delimiter''tab')type'm_tab.txt'
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

Create a matrix, write it to a spreadsheet file, and then read and display the contents of the file.

Create a matrix in the workspace.

m =魔术(5)
M =5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

将矩阵写入电子表格文件。

Writematrix(M,'M.xls'

Read and display the matrix fromM.xls

Readmatrix('M.xls'
ans =.5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

创建一个矩阵并将其写入指定的表格和电子表格文件中的范围。

Create a matrix in the workspace.

m =魔术(5)
M =5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

Write the matrix toM.xls那to the second worksheet in the file, starting at the third row.

Writematrix(M,'M.xls''Sheet'那2那'范围''一种3:E8'

读取并显示矩阵。

Readmatrix('M.xls''Sheet'那2那'范围''一种3:E8'
ans =.5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

一种ppend an array of data below existing data in a spreadsheet.

Create two matrices in the workspace.

m1 =魔术(5)
m1 =5×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
m2 =[5 10 15 20 25; 30 35 40 45 50]
m2 =2×55 10 15 20 25 30 35 40 45 50

Write the matrixM1to a spreadsheet file,M.xls.

writematrix(M1,'M.xls'

追加数据矩阵M2below the existing data in the spreadsheet file.

writematrix(M2,'M.xls''WriteMode''append'

Read the spreadsheet file and display the matrix.

Readmatrix('M.xls'
ans =.7×517 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 5 10 15 20 25 30 35 40 45 50

一种ppend an array of data below existing data in a text file.

Create two matrices in the workspace.

fibonacci1 = [1 1 2 3;5 8 13 21;34 55 89 144]
fibonacci1 =3×41 1 2 3 5 8 13 21 34 50 89 144
fibonacci2 = [233 377 610 987]
fibonacci2 =1×4233 377 610 987

Write the matrixfibonacci1到文本文件,fibonacci.txt.

Writematrix(Fibonacci1,'fibonacci.txt'

追加数据fibonacci2现有以下文本文件中的数据。

writematrix(fibonacci2,'fibonacci.txt''WriteMode''append'

Read the text file and display the matrix.

Readmatrix('fibonacci.txt'
ans =.4×41 1 2 3 5 8 13 21 34 50 89 144233 377 610 987

输入参数

collapse all

Input data, specified as a matrix.

File name, specified as a character vector or string scalar.

Depending on the location you are writing to,filename可以采取其中一种形式。

Location

形式

Current folder

To write to the current folder, specify the name of the file infilename

Example:'mytextfile.csv'

Other folders

To write to a folder different from the current folder, specify the full or relative path name infilename

Example:'C:\myFolder\myTextFile.csv'

Example:'myFolder\myExcelFile.xlsx'

偏远的地点

To write to a remote location,filenamemust contain the full path of the file specified as a uniform resource locator (URL) of the form:

scheme_name://path_to_file/my_file.ext

基于远程位置,scheme_namecan be one of the values in this table.

偏远的地点 scheme_name
一种mazon S3™ s3
Windows Azure.®Blob Storage wasbwasbs
HDFS™ hdfs

For more information, seeWork with Remote Data

Example:'s3://bucketname/path_to_file/my_file.xlsx'

  • 如果filenameincludes the file extension, then the writing function determines the file format from the extension. Otherwise, the writing function creates a comma separated text file and appends the extension。txt。或者,您可以指定filename没有文件的扩展名,然后包括'文件类型'的名字-value pair arguments to indicate the type of file.

  • 如果filename不存在,然后写作函数创建文件。

  • 如果filenameis the name of an existing text file, then the writing function overwrites the file.

  • 如果filenameis the name of an existing spreadsheet file, then the writing function writes the data to the specified location, but does not overwrite any values outside the range of the input data.

Data Types:char|string

Name-Value Pair Arguments

指定可选的逗号分离对名称,价值arguments.Name是参数名称和Valueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN

Example:'文件类型'那textindicates that the variable names should not be included as the first row of the output file.
Text and Spreadsheet Files

collapse all

文件类型,指定为逗号分隔对组成'文件类型'and a character vector or string containing'text'or'spreadsheet'

'文件类型'的名字-value pair must be used with thefilenameinput argument. You do not need to specify the'文件类型'的名字-value pair argument if thefilenameinput argument includes a standard file extension. The following standard file extensions are recognized by the writing function:

  • 。txt.DAT.那or。csvfor delimited text files

  • .xls..xls.m那or.xls.xfor Excel spreadsheet files

  • .xls.bfor Excel spreadsheet files supported on systems with Excel for Windows

Example:'filetype','电子表格'

Data Types:char|string

Locale for writing dates, specified as the comma-separated pair consisting of'datelocale'and a character vector or a string scalar. When writingdatetime文件的值,使用DateLocaleto specify the locale in whichwritematrixshould write month and day-of-week names and abbreviations. The character vector or string takes the formxx_YY那wherexxis a lowercase ISO 639-1 two-letter code indicating a language, andYY是一个大写ISO 3166-1 Alpha-2代码,指示一个国家。有关区域设置的常见值列表,请参阅Locale的名字-value pair argument for thedatetime功能。

这writing function ignores the'datelocale'parameter value whenever dates can be written as Excel-formatted dates.

Example:'datelocale'那'ja_JP'

Data Types:char|string

Writing mode, specified as the comma-separated pair consisting of'WriteMode'and a character vector or a string scalar. Select a write mode based on the file type.

File Type

写模式

Text Files

  • 'overwrite'(default) — Overwrite the file.

  • 'append'-一种ppend data to the file.

如果the file you specified does not exist, then the writing function creates and writes data to a new file.

Spreadsheet Files

  • 'inplace'(default) — Update only the range occupied by the input data. The writing function does not alter any data outside of the range occupied by the input data.

    • 如果you do not specify a sheet, then the writing function writes to the first sheet.

  • 'overwritesheet'- 清除指定的纸张并将输入数据写入清除表。

    • 如果未指定表格,则写入功能清除第一个纸张并将输入数据写入其中。

  • 'append'- 写入函数将输入数据附加到指定表的占用范围的底部。

    • 如果you do not specify a sheet, then the writing function appends the input data to the bottom of the occupied range of the first sheet.

  • 'replacefile'- 从文件中删除所有其他工作表,然后清除并将输入数据写入指定的工作表。

    • 如果未指定表格,则写入函数从文件中删除所有其他工作表,然后清除并将输入数据写入第一张。

    • 如果不存在您指定的文件,则写入功能创建一个新文件并将输入数据写入第一个纸张。

  • Whenwritevariablenames.is set totrue,写入功能不支持写入模式金宝app'append'

  • For spreadsheet files:

    • 当写模式是'append'那the writing function does not support theRangeparameter.

    • 如果the file you specified does not exist, then the writing function performs the same actions as'replacefile'

Example:'WriteMode','append'

Data Types:char|string

Text Files Only

collapse all

Field delimiter character, specified as the comma-separated pair consisting of'delimiter'and a character vector or string scalar containing one of these specifiers:

Specifier

Field Delimiter

'那'

'逗号'

逗号。这是默认行为。

''

'space'

空间

'\t'

'tab'

Tab

';'

'semi'

分号

'|'

'bar'

Vertical bar

You can use the'delimiter'的名字-value pair only for delimited text files.

Example:'delimiter','space'

Data Types:char|string

Indicator for writing quoted text, specified as the comma-separated pair consisting of'QuoteStrings'and either错误的ortrue。如果'QuoteStrings'istrue, 然后the writing function encloses the text in double quotation marks and replaces any double-quote characters that appear as part of that text with two double-quote characters.

You can use the'QuoteStrings'的名字-value pair only with delimited text files.

与文件相关联的字符编码方案,指定为包含的逗号分隔对'编码'and'system'or a standard character encoding scheme name. When you do not specify any encoding, the writing function uses UTF-8 to write the file.

Example:'编码'那'UTF-8'uses UTF-8 as the encoding.

Data Types:char|string

Spreadsheet Files Only

collapse all

Worksheet to write to, specified as the comma-separated pair consisting of'Sheet'and a character vector or a string scalar containing the worksheet name or a positive integer indicating the worksheet index. The worksheet name cannot contain a colon ()。To determine the names of sheets in a spreadsheet file, usesheets = sheetnames(filename)。For more information, seesheetnames

Specify the worksheet to write to by name or index:

  • 的名字-如果the specified sheet name does not exist in the file, then the writing function adds a new sheet at the end of the worksheet collection.

  • 索引 - 如果指定的纸张索引是索引大于工作表的数量,则写入函数追加空工作表,直到工作簿中的工作表数量等于纸张索引。写入功能还生成警告,指示它已添加新工作表。

You can use the'Sheet'的名字-value pair only with spreadsheet files.

Example:'Sheet'2

Example:'Sheet''MySheetName'

Data Types:char|string|single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

要写入的工作表的矩形部分那specified as the comma-separated pair consisting of'范围'and a character vector or string scalar in one of the following forms.

价值的形式Range Description
'角落1.'

角落1.specifies the first cell of the region to write. The writing function writes the data starting at this cell.

Example:'范围'那'D2'

'角落1.:角落2.'

角落1.and角落2.are two opposing corners that define the region to write. For example,'D2:H4.'代表两个角之间的3×5矩形区域D2andH4.on the worksheet. The'范围'名称值对参数不区分大小写,并使用Excel A1参考样式(请参阅Excel帮助)。

Example:'范围'那'D2:H4.'

  • 如果the range you specify is smaller than the size of the input data, then the writing function writes only a subset of the input data that fits into the range.

  • 如果the range you specify is larger than the size of the input data, then the writing function leaves the remainder of the region as it is.

'范围'的名字-value pair can only be used with Excel files.

Example:'范围''一种1:F10'

Data Types:char|string

旗帜开始一个实例微软®Excel对于编写电子表格数据时,对于Windows,指定为逗号分隔对组成'meftexcel.'and eithertrue那or错误的

你可以设置'meftexcel.'parameter to one of these values:

  • true- 写入函数在编写文件时启动Microsoft Excel的实例。

  • 错误的-这writing function does not start an instance of Microsoft Excel when writing the file. When operating in this mode, functionality for writing differs in the support of file formats and interactive features, such as formulas and macros.

meftexcel.

true

错误的

Supported file formats

xls, .xlsx、.xlsm .xltx、.xltm .xlsb。ods

.xls,.xlsx,.xlsm,.xltx,.xltm

金宝app支持交互式功能,例如公式和宏

Yes

No

在Windows平台上写入电子表格文件时,如果要启动一个实例微软Excel,然后设置'meftexcel.'parameter totrue

自动调整列宽,指定为trueor错误的。如果you specify a value of0.or错误的, 然后writematrixwill not automatically adjust the column widths to fit the data in the cells.

Example:'autofitwidth'0.

Preserve cell formatting of existing spreadsheet, specified astrueor错误的。如果you specify错误的writematrixwill not preserve the cell formatting of the spreadsheet. Formatting includes elements such as fonts, cell borders, and color-shaded cells.

When writingdatetime数据到电子表格文件,必须设置两者'PreserveFormat'and the'meftexcel.'Name-Value pair totrue保留现有的单元格格式。如果'meftexcel.'is set to错误的and'PreserveFormat'is set totruewhen writingdatetime数据到文件,writematrix不会保留文件的现有单元格格式。

Example:'PreserveFormat'错误的

Limitations

  • To set the'PreserveFormat'的名字-value pair totrue那you must set the'meftexcel.'的名字-value pair totrue

一种lgorithms

有一些实例在哪里writematrixfunction creates a file that does not represent the input data exactly. You will notice this when you use thereadmatrixfunction to read that file. The resulting data might not have the exact same format or contents as the original array. If you need to save your array and retrieve it at a later time to match the original array exactly, with the same data and organization, then save it as a MAT-file.writematrixwrites inexact data in the following instances:

  • writematrixwrites out numeric data usinglong gformat, and categorical or character data as unquoted text.

  • writematrix将阵列写入两个尺寸作为二维阵列以上的阵列,尾随尺寸倒塌。

在R2019A介绍