Writematrix
Write a matrix to a file
Description
Writematrix(
writes homogeneous arrayA
)A
to a comma delimited text file. The file name is the workspace variable name of the array, appended with the extension。文本
。IfWritematrix
无法从数组名称构造文件名,然后将其写入文件matrix.txt
。
Each column of each variable inA
成为输出文件中的列。这Writematrix
function overwrites any existing file.
Writematrix(
写信给具有指定名称和扩展名的文件A
,filename
)filename
。
Writematrix
determines the file format based on the specified extension. The extension must be one of the following:
。文本
,。dat
, or。csv
for delimited text files。xls
,.xlsm
, or。xlsx
对于Excel®spreadsheet files.xlsb
对于Excelspreadsheet files supported on systems with Excel for Windows®
Writematrix(___,
将数组写入文件,其中包含一个或多个指定的其他选项Name,Value
)Name,Value
pair arguments and can include any of the input arguments in previous syntaxes.
Examples
Write Matrix to Text File
Create a matrix, write it to a comma-separated text file, and then write the matrix to another text file with a different delimiter character.
在工作区中创建矩阵。
M = magic(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
function outputs a text file namedM.txt
。
Writematrix(M) type'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
To write the same matrix to a text file with a different delimiter character, use the'Delimiter'
name-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
将矩阵写入电子表格文件
创建一个矩阵,将其写入电子表格文件,然后读取并显示文件的内容。
在工作区中创建矩阵。
M = magic(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
写矩阵to a spreadsheet file.
Writematrix(M,'M.xls')
阅读并显示矩阵M.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
Write Matrix to Specified Sheet and Range
Create a matrix and write it to a specified sheet and range in a spreadsheet file.
在工作区中创建矩阵。
M = magic(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
写矩阵toM.xls
, to the second worksheet in the file, starting at the third row.
Writematrix(M,'M.xls','Sheet',2,'Range','A3:E8')
Read and display the matrix.
readmatrix('M.xls','Sheet',2,'Range','A3: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
Append Data to Spreadsheet
在电子表格中的现有数据下方附加一系列数据。
Create two matrices in the workspace.
M1 = magic(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
写矩阵M1
to a spreadsheet file,M.xls
。
Writematrix(M1,'M.xls')
Append the data in matrixM2
在电子表格文件中的现有数据下方。
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
Append Matrix Data to Text File
Append 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 55 89 144
fibonacci2 = [233 377 610 987]
fibonacci2 =1×4233 377 610 987
写矩阵fibonacci1
to a text file,fibonacci.txt
。
Writematrix(fibonacci1,'fibonacci.txt')
Append the data infibonacci2
在文本文件中的现有数据下方。
Writematrix(fibonacci2,'fibonacci.txt',“ writemode','append')
阅读文本文件并显示矩阵。
readmatrix('fibonacci.txt')
ans =4×41 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
Input Arguments
A
—输入数据
matrix
输入数据, specified as a matrix.
filename
—File name
character vector|string scalar
File name, specified as a character vector or string scalar.
Depending on the location you are writing to,filename
can take on one of these forms.
Location |
Form |
||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder | 要写入当前文件夹,请在 Example: |
||||||||
Other folders |
To write to a folder different from the current folder, specify the full or relative path name in Example: Example: |
||||||||
Remote Location |
要写入远程位置,
Based on the remote location,
For more information, seeWork with Remote Data。 Example: |
If
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。文本
。Alternatively, you can specifyfilename
without the file’s extension, and then include the'FileType'
name-value pair arguments to indicate the type of file.
If
filename
does not exist, then the writing function creates the file.If
filename
is the name of an existing text file, then the writing function overwrites the file.If
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
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
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 encloseName
in quotes.
Example:'FileType',text
indicates that the variable names should not be included as the first row of the output file.
FileType
—Type of file
'text'
|'spreadsheet'
Type of file, specified as the comma-separated pair consisting of'FileType'
以及包含字符向量或字符串'text'
or'spreadsheet'
。
这'FileType'
name-value pair must be used with thefilename
input argument. You do not need to specify the'FileType'
name-value pair argument if thefilename
input argument includes a standard file extension. The following standard file extensions are recognized by the writing function:
。文本
,。dat
, or。csv
for delimited text files。xls
,.xlsm
, or。xlsx
对于Excelspreadsheet files.xlsb
对于Excelspreadsheet files supported on systems with Excel for Windows
Example:'FileType','spreadsheet'
Data Types:char
|string
DateLocale
—Locale for writing dates
character vector|string scalar
Locale for writing dates, specified as the comma-separated pair consisting of'DateLocale'
以及字符向量或字符串标量。写作时datetime
values to the file, useDateLocale
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
is an uppercase ISO 3166-1 alpha-2 code indicating a country. For a list of common values for the locale, see theLocale
name-value pair argument for thedatetime
function.
这writing function ignores the'DateLocale'
参数值每当可以写入Excel-formatted日期时。
Example:'DateLocale','ja_JP'
Data Types:char
|string
WriteMode
—Writing mode
character vector|string scalar
Writing mode, specified as the comma-separated pair consisting of“ writemode'
以及字符向量或字符串标量。Select a write mode based on the file type.
File Type |
Write Mode |
---|---|
Text Files |
If the file you specified does not exist, then the writing function creates and writes data to a new file. |
Spreadsheet Files |
|
什么时候
WriteVariableNames
被设定为true
, the writing function does not support the write mode'append'
。For spreadsheet files:
什么时候the write mode is
'append'
, the writing function does not support theRange
parameter.If 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
—字段定界符字符
character vector|string scalar
字段定界符字符, specified as the comma-separated pair consisting of'Delimiter'
and a character vector or string scalar containing one of these specifiers:
说明符 |
字段定界符 |
---|---|
|
Comma. This is the default behavior. |
|
Space |
|
标签 |
|
Semicolon |
|
Vertical bar |
You can use the'Delimiter'
name-value pair only for delimited text files.
Example:'Delimiter','space'
Data Types:char
|string
QuoteStrings
—Indicator for writing quoted text
"minimal"
(default) |“全部”
|"none"
Indicator for writing quoted text, specified as"minimal"
,“全部”
, or"none"
。
If
QuoteStrings
is"minimal"
, then the function encloses any variables containing the delimiter, line ending, or double-quote character in double-quote characters.If
QuoteStrings
is“全部”
, then the function encloses all text, categorical, datetime, and duration variables in double-quote characters.If
QuoteStrings
is"none"
,然后该函数不会将变量包装在双引号字符中。
You can use theQuoteStrings
名称值参数仅带有划界文本文件。
Encoding
—字符编码方案
'UTF-8'
(default) |'ISO-8859-1'
|'windows-1251'
|'windows-1252'
| ...
字符编码方案associated with the file, specified as the comma-separated pair consisting of'Encoding'
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:'Encoding','UTF-8'
uses UTF-8 as the encoding.
Data Types:char
|string
Sheet
—Worksheet to write to
character vector|string scalar|positive integer
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:
name — If 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.
index — If the specified sheet index is an index larger than the number of worksheets, then the writing function appends empty sheets until the number of worksheets in the workbook equals the sheet index. The writing function also generates a warning indicating that it has added a new worksheet.
You can use the'Sheet'
name-value pair only with spreadsheet files.
Example:'Sheet'
,2
Example:'Sheet'
,'MySheetName'
Data Types:char
|string
|单
|double
|int8
|INT16
|INT32
|int64
|uint8
|uint16
|uint32
|uint64
Range
—Rectangular portion of worksheet to write to
character vector|string scalar
矩形工作表的矩形部分要写入,指定为逗号分隔对'Range'
and a character vector or string scalar in one of the following forms.
表单的值ofRange |
Description |
---|---|
' |
Example: |
' |
Example:
|
这'Range'
名称值对只能与Excel文件一起使用。
Example:'Range'
,'A1:F10'
Data Types:char
|string
UseExcel
—国旗风启动Microsoft Excel的实例ows
false
(default) |true
Flag to start an instance ofMicrosoft®Excelfor Windows when writing spreadsheet data, specified as the comma-separated pair consisting of'UseExcel'
and eithertrue
, orfalse
。
You can set the'UseExcel'
parameter to one of these values:
true
— The writing function starts an instance of Microsoft Excel when writing the file.false
— The 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.
UseExcel |
|
|
---|---|---|
万博1manbetx支持的文件格式 |
|
|
Support for interactive features, such as formulas and macros |
Yes |
No |
写作时to spreadsheet files on Windows platforms, if you want to start an instance ofMicrosoft Excel, then set the'UseExcel'
parameter totrue
。
自动放宽
—Automatically adjust column width
true
(default) |false
Automatically adjust column width, specified astrue
orfalse
。如果指定a value of0
orfalse
, thenWritematrix
will not automatically adjust the column widths to fit the data in the cells.
Example:'AutoFitWidth'
,0
PreserveFormat
—Preserve cell formatting
true
(default) |false
Preserve cell formatting of existing spreadsheet, specified astrue
orfalse
。如果指定false
,Writematrix
will not preserve the cell formatting of the spreadsheet. Formatting includes elements such as fonts, cell borders, and color-shaded cells.
写作时datetime
data to a spreadsheet file, you must set both'PreserveFormat'
and the'UseExcel'
Name-Value pair totrue
to preserve the existing cell formatting. If'UseExcel'
被设定为false
and'PreserveFormat'
被设定为true
写作时datetime
data to the file,Writematrix
will not preserve the existing cell formatting of the file.
Example:'PreserveFormat'
,false
Limitations
To set the
'PreserveFormat'
名称对true
, you must set the'UseExcel'
名称对true
。
Algorithms
这re are some instances where theWritematrix
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
writes out arrays that have more than two dimensions as two dimensional arrays, with the trailing dimensions collapsed.
Version History
See Also
Beispiel öffnen
Sie haben eine geänderte Version dieses Beispiels. Möchten Sie dieses Beispiel mit Ihren Änderungen öffnen?
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
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:。
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- AméricaLatina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- 丹麦(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)