How do I add column headers to a spreadsheet in MATLAB after converting .mat file to .xlsx file?
조회 수: 28 (최근 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
댓글 수: 0
답변 (2개)
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!