How do I add column headers to a spreadsheet in MATLAB after converting .mat file to .xlsx file?

34 次查看(过去 30 天)
I am trying to add column headers (1x33) to a matrix of data (##x33, numbers of columns depends). Right now I am simply opening the spreadsheet and adding column headers by hand after converting .mat file to .xlsx file. My questions are:
1) Is it easier to combine the column header matrix to the data matrix before or after using the xlswrite command to generate a spreadsheet from the .mat file? 2) Depending on the above answer, what code is needed to do this and where it would into the code found below?
Current Code to save matrix in workspace as .mat file and then write data into spreadsheet:
save('\directory\filename.mat')
data = load('\directory\filename.mat'); f = fieldnames(data);
for k=1:size(f,1)
xlswrite('newfilename.xlsx',data.(f{k}),f{k})
end

回答(2 个)

Fangjun Jiang
Fangjun Jiang 2016-8-11
Use table object would be better for this.
help table

Jade Sen
Jade Sen 2017-3-17
编辑:Jade Sen 2017-3-17
For this you must use xlswrite function before you add your parameters or structure properties to the table :
excelFileName='TestExcelFile.xlsx';
header={'Name','Data Type','Value','Minimum','Maximum', 'Units' , ... 'Storage Class'}; %etc
xlswrite(excelFileName,header,1); %1 is the sheet number which is by default 1
% then with your piece of code just keep updating rows for eg:
rowNumber=2; % or 3 if you need spacing between header and data % row number 1 is reserved for our header for k=1:size(f,1) xlswrite(excelFileName,data.(f{k},1,['A' num2str(rowNumber)]); %1 is the sheet number rowNumber=rowNumber+1; end
  1 个评论
Sandeep GNV
Sandeep GNV 2021-8-18
hi i have the similar kind of problem. i have set of MAT files ina folder so i wrote an automation code to convert the set of MAT files in folder to CSV files all at a time. each MAT file consits of 2 datasets (Inputs and Outputs).
for i = 1:4; for j = 0:50:100
base=sprintf('Heating_Data1_0C_RB%d_Comb%d',j,i);
S=load([base '.mat']);
data=[S.([base '_Inputs']) S.([base '_Outputs'])];
xlswrite([base '.csv'],data);
end
end
so this merge the two MAT files(Inputs &Outputs) into a single MAT file and covnerts it to CSV file. but here in the excel sheet you see only the numeric data, so now i need to insert a Row on the top with the column headings.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by