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

42 ビュー (過去 30 日間)
S_G
S_G 2016 年 8 月 11 日
コメント済み: Sandeep GNV 2021 年 8 月 18 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by