MATLAB Answers

How do I use XLSWRITE to add row and column labels to my MATLAB matrix when I write it to Excel in MATLAB 7.3 (R2006b)?

653 views (last 30 days)
I am using XLSWRITE to write numeric data to Excel. I would like to include text row and column headers to the matrix when I write it to Excel.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The following examples illustrate how to use XLSWRITE to add column and row labels to a MATLAB matrix that is written to an Excel file. This can be done either by writing to several sections of the worksheet (Case 1), or by joining the data and labels before writing to the XLS-file (Case 2).
Case 1. Using several XLSWRITE commands:
data=ones(10,4); %Sample 2-dimensional data
col_header={'Temperature','Pressure','X','Y'}; %Row cell array (for column labels)
row_header(1:10,1)={'Time'}; %Column cell array (for row labels)
xlswrite('My_file.xls',data,'Sheet1','B2'); %Write data
xlswrite('My_file.xls',col_header,'Sheet1','B1'); %Write column header
xlswrite('My_file.xls',row_header,'Sheet1','A2'); %Write row header
Case 2. Using one XLSWRITE command:
data=ones(10,4); %Sample 2-dimensional data
data_cells=num2cell(data); %Convert data to cell array
col_header={'Temperature','Pressure','X','Y'}; %Row cell array (for column labels)
row_header(1:10,1)={'Time'}; %Column cell array (for row labels)
output_matrix=[{' '} col_header; row_header data_cells]; %Join cell arrays
xlswrite('My_file.xls',output_matrix); %Write data and both headers
  2 Comments
Mustafa Ahmed
Mustafa Ahmed on 7 May 2021
i need your help
i have a code to get me a result as EXCEL file with 184 rows and 7 column
i tried your code to lable the columns but it did not work
it shows an error
Error using horzcat
Dimensions of arrays being concatenated are not
consistent.
Error in Untitled22222SOC (line 5)
output_matrix=[{' '} col_header; row_header
data_cells]; %Join cell arrays

Sign in to comment.

Tags

No tags entered yet.

Products


Release

R2006b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!