Write Data toExcelSpreadsheets
Write Tabular Data to Spreadsheet File
To export a table in the workspace to a Microsoft® Excel® spreadsheet file, use thewritetable
function. You can export data from the workspace to any worksheet in the file, and to any location within that worksheet. By default,writetable
writes 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
to the first sheet in a new spreadsheet file namedpatientdata.xlsx
, starting at cellD1
. To specify the portion of the worksheet you want to write to, use the范围
名称-值对基于“增大化现实”技术gument. By default,writetable
writes the table variable names as column headings in the spreadsheet file.
filename ='patientdata.xlsx'; writetable(T,filename,'Sheet',1,'范围','D1')
Write the tableT
without the variable names to a new sheet called“MyNewSheet”
. To write the data without the variable names, specify the name-value pairWriteVariableNames
asfalse
.
writetable(T,filename,'Sheet',“MyNewSheet”,'WriteVariableNames',false);
Write Numeric and Text Data to Spreadsheet File
To export a numeric array and a cell array to aMicrosoft®Excel®spreadsheet file, use thewritematrix
orwritecell
functions. 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','Temp'; 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 arrayA
to 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,'范围','E1:I5')
Write cell arrayC
to a rectangular region that starts at cellB2
on a worksheet named温度
. You can specify range using only the first cell.
writecell(C,filename,'Sheet','温度','范围','B2');
writecell
displays 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 thewritetable
andwritecell
函数显示这个警告:
Warning: Added specified worksheet.
For information on how to suppress warning messages, seeSuppress Warnings.
Format Cells inExcelFiles
To write data to Excel files on Windows®systems with custom formats (such as fonts or colors), access the COM server directly usingactxserver
rather thanwritetable
,writetimetable
,writematrix
, orwritecell
. For example,Technical Solution 1-QLD4Kusesactxserver
to establish a connection between MATLAB®and Excel, write data to a worksheet, and specify the colors of the cells.
For more information, seeGet Started with COM.