How do I add column headers to a spreadsheet in MATLAB after converting .mat file to .xlsx file?
27 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Jade Sen
on 17 Mar 2017
Edited: Jade Sen
on 17 Mar 2017
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 Comment
Sandeep GNV
on 18 Aug 2021
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.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!