主要内容

Write Data toExcelSpreadsheets

Write Tabular Data to Spreadsheet File

To export a table in the workspace to a Microsoft® Excel® spreadsheet file, use thewritetable功能。您可以将数据从工作空间导出到文件中的任何工作表,以及该工作表中的任何位置。默认,writetablewrites your table data to the first worksheet in the file, starting at cellA1.

For example, create a sample table of column-oriented data and display the first five rows.

loadpatients.matT = table(LastName,Age,Weight,Smoker); T(1:5,:)
ans=5×4 tableLastName Age Weight Smoker ____________ ___ ______ ______ {'Smith' } 38 176 true {'Johnson' } 43 163 false {'Williams'} 38 131 false {'Jones' } 40 133 false {'Brown' } 49 119 false

Write tableT到名称的新电子表格文件中的第一张patientdata.xlsx, starting at cellD1. To specify the portion of the worksheet you want to write to, use the范围名称-值对基于“增大化现实”技术gument. By default,writetablewrites the table variable names as column headings in the spreadsheet file.

filename ='patientdata.xlsx';撰写(t,文件名,'Sheet',1,'Range','d1')

Write the tableTwithout the variable names to a new sheet called'mynewsheet'. To write the data without the variable names, specify the name-value pairWriteVariableNamesasfalse.

撰写(t,文件名,'Sheet','mynewsheet','Write Variablenames',错误的);

Write Numeric and Text Data to Spreadsheet File

To export a numeric array and a cell array to aMicrosoft®Excel®spreadsheet file, use theWritematrix或者Writecellfunctions. You can export data in individual numeric and text workspace variables to any worksheet in the file, and to any location within that worksheet. By default, the import functions write your matrix data to the first worksheet in the file, starting at cellA1.

For example, create a sample array of numeric data,A, and a sample cell array of text and numeric data,C.

A = magic(5) C = {'Time',“临时”;12 98; 13'x';14 97}
A = 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 C = 'Time' 'Temp' [ 12] [ 98] [ 13] 'x' [ 14] [ 97]

Write arrayAto the 5-by-5 rectangular region,E1:I5, on the first sheet in a new spreadsheet file namedtestdata.xlsx.

filename ='testdata.xlsx';Writematrix(A,filename,'Sheet',1,'Range','E1:I5')

写单元阵列Cto a rectangular region that starts at cellB2on a worksheet named温度. You can specify range using only the first cell.

Writecell(C,filename,'Sheet','Temperatures','Range','B2');

Writecelldisplays a warning because the worksheet,温度, did not previously exist, but you can disable this warning.

Disable Warning When Adding New Worksheet

If the target worksheet does not exist in the file, then thewritetableWritecell函数显示这个警告:

警告:添加了指定的工作表。

For information on how to suppress warning messages, seeSuppress Warnings.

Format Cells inExcelFiles

将数据写入Windows上的Excel文件®systems with custom formats (such as fonts or colors), access the COM server directly usingactxserverrather thanwritetable,writetimetable,Writematrix, orWritecell. For example,Technical Solution 1-QLD4K用途actxserver建立MATLAB之间的连接®和Excel, write data to a worksheet, and specify the colors of the cells.

For more information, see开始使用com.

See Also

||