Convert .mat to excel file adding column headers

조회 수: 15 (최근 30일)
Callum Swann
Callum Swann 2020년 4월 29일
답변: Vinai Datta Thatiparthi 2020년 5월 1일
I am trying to convert a .mat file to an excel file, and during this is would like to add headers to the columns of the excel sheet.
my .mat file has 3 variables (a,b,c) and I would like each variable to have its own sheet in the excel file.
I would like the headers of 'sheet a' to be (time, distance, speed), 'sheet b' to be (time, acceleration, force) and 'sheet c' to be (time, lift, drag)
How would i do this?

답변 (1개)

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi 2020년 5월 1일
Hey Callum,
% Since the .mat file was not provided, I'm generating a bunch of random numbers for the variables
a = randi(10,[10,3]);
b = randi(10,[10,3]);
c = randi(10,[10,3]);
% Cell array of headers corresponding to the variables
headerA = {'time','distance','speed'};
headerB = {'time','acceleration','force'};
headerC = {'time','lift','drag'};
% Create a cell array containing both the data and the headers
out = cell(1,3);
out{1} = [headerA; num2cell(a)];
out{2} = [headerB; num2cell(b)];
out{3} = [headerC; num2cell(c)];
% Use the function 'writecell' to export your data into an Excel sheet
for idx=1:3
writecell(out{idx},'test.xlsx','Sheet',idx);
end
Find the Documentation for writecell here: LINK
Hope this helps!

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by