向文件写一个矩阵
writematrix(
writes homogeneous array一种
)一种
逗号分隔的文本文件。文件名是阵列的Workspace变量名称,附加到扩展名。txt
。如果writematrix
cannot construct the file name from the array name, then it writes to the file矩阵。txt
。
每个变量的每个列一种
becomes a column in the output file. Thewritematrix
function overwrites any existing file.
writematrix(
writes to a file with the name and extension specified by一种
那filename
)filename
。
writematrix
determines the file format based on the specified extension. The extension must be one of the following:
。txt
那.DAT.
那or。csv
for delimited text files
.xls.
那.xls.m
那or.xls.x
for Excel®电子表格文件
.xls.b
for Excel spreadsheet files supported on systems with Excel for Windows®
创建一个矩阵,将其写入逗号分隔的文本文件,然后用不同的分隔符字符将矩阵写入另一个文本文件。
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. Thewritematrix
function 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 matrixM1
to a spreadsheet file,M.xls.
writematrix(M1,'M.xls')
追加数据矩阵M2
below 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
一种
-Input dataInput data, specified as a matrix.
filename
-File nameFile 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 in Example: |
||||||||
Other folders |
To write to a folder different from the current folder, specify the full or relative path name in Example: Example: |
||||||||
偏远的地点 |
To write to a remote location,
基于远程位置,
For more information, seeWork with Remote Data。 Example: |
如果filename
includes 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
不存在,然后写作函数创建文件。
如果filename
is the name of an existing text file, then the writing function overwrites the file.
如果filename
is 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
指定可选的逗号分离对名称,价值
arguments.Name
是参数名称和Value
is the corresponding value.Name
must appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN
。
'文件类型'那text
indicates that the variable names should not be included as the first row of the output file.
'文件类型'
-Type of file'text'
|'spreadsheet'
文件类型,指定为逗号分隔对组成'文件类型'
and a character vector or string containing'text'
or'spreadsheet'
。
这'文件类型'
的名字-value pair must be used with thefilename
input argument. You do not need to specify the'文件类型'
的名字-value pair argument if thefilename
input argument includes a standard file extension. The following standard file extensions are recognized by the writing function:
。txt
那.DAT.
那or。csv
for delimited text files
.xls.
那.xls.m
那or.xls.x
for Excel spreadsheet files
.xls.b
for Excel spreadsheet files supported on systems with Excel for Windows
Example:'filetype','电子表格'
Data Types:char
|string
'datelocale'
-Locale for writing datesLocale for writing dates, specified as the comma-separated pair consisting of'datelocale'
and a character vector or a string scalar. When writingdatetime
文件的值,使用DateLocale
to specify the locale in whichwritematrix
should write month and day-of-week names and abbreviations. The character vector or string takes the form
那wherexx
_YY
xx
is 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
'WriteMode'
-Writing modeWriting 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 |
如果the file you specified does not exist, then the writing function creates and writes data to a new file. |
Spreadsheet Files |
|
Whenwritevariablenames.
is set totrue
,写入功能不支持写入模式金宝app'append'
。
For spreadsheet files:
当写模式是'append'
那the writing function does not support theRange
parameter.
如果the file you specified does not exist, then the writing function performs the same actions as'replacefile'
。
Example:'WriteMode','append'
Data Types:char
|string
'delimiter'
-Field delimiter characterField 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 |
---|---|
|
逗号。这是默认行为。 |
|
空间 |
|
Tab |
|
分号 |
|
Vertical bar |
You can use the'delimiter'
的名字-value pair only for delimited text files.
Example:'delimiter','space'
Data Types:char
|string
'QuoteStrings'
-Indicator for writing quoted text错误的
(default) |true
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.
'编码'
-Character encoding scheme'UTF-8'
(default) |'ISO-8859-1'
|'windows-1251'
|'windows-1252'
|。。。与文件相关联的字符编码方案,指定为包含的逗号分隔对'编码'
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
'Sheet'
-Worksheet to write toWorksheet 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 |
---|---|
' |
Example: |
' |
Example:
|
这'范围'
的名字-value pair can only be used with Excel files.
Example:'范围'
那'一种1:F10'
Data Types:char
|string
'meftexcel.'
-Flag to start instance of Microsoft Excel for Windows错误的
(default) |true
旗帜开始一个实例微软®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. |
|
|
---|---|---|
Supported file formats |
|
|
金宝app支持交互式功能,例如公式和宏 |
Yes |
No |
在Windows平台上写入电子表格文件时,如果要启动一个实例微软Excel,然后设置'meftexcel.'
parameter totrue
。
'autofitwidth'
-自动调整列宽true
(default) |错误的
自动调整列宽,指定为true
or错误的
。如果you specify a value of0.
or错误的
, 然后writematrix
will not automatically adjust the column widths to fit the data in the cells.
Example:'autofitwidth'
那0.
'PreserveFormat'
-Preserve cell formattingtrue
(default) |错误的
Preserve cell formatting of existing spreadsheet, specified astrue
or错误的
。如果you specify错误的
那writematrix
will 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 totrue
when writingdatetime
数据到文件,writematrix
不会保留文件的现有单元格格式。
Example:'PreserveFormat'
那错误的
To set the'PreserveFormat'
的名字-value pair totrue
那you must set the'meftexcel.'
的名字-value pair totrue
。
有一些实例在哪里writematrix
function creates a file that does not represent the input data exactly. You will notice this when you use thereadmatrix
function 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.writematrix
writes inexact data in the following instances:
writematrix
writes out numeric data usinglong g
format, and categorical or character data as unquoted text.
writematrix
将阵列写入两个尺寸作为二维阵列以上的阵列,尾随尺寸倒塌。
You have a modified version of this example. Do you want to open this example with your edits?
您单击了与此MATLAB命令对应的链接:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:。
Selectweb siteYou can also select a web site from the following list:
选择中国网站(以中文或英文)以获取最佳网站性能。其他MathWorks国家网站未优化您的位置。