Datetime conversion to table headings

조회 수: 3 (최근 30일)
mabinj
mabinj 2019년 3월 14일
댓글: Adam Danz 2019년 3월 14일
I have two matrices x ~ [3733x1], y ~[3733 x 498] and time ~ 498 x 1 datetime.
I want to be able to write an excel file with x in the first column, and y as subsequent columns. As each dataset(column) in y is at a specific time, I want to put the times as the headers.
I know timetable does this but puts the time stamps in the first row, is there an obvious alternative to timetable that will put the timestamps as headers?
raw_data= [x y];
array2timetable(raw_data, 'RowTimes', time);
T = table(x,y);
T.Properties.VariableNames{2:end} = {Time};
Here is what I've currently tried, but as you can see the 'Rowtimes' is not what I want, so I attempted to create a table and change the variable names but that equally hasn't worked. Am I missing something painfully obvious?

채택된 답변

Adam Danz
Adam Danz 2019년 3월 14일
편집: Adam Danz 2019년 3월 14일
If your goal is to write to an excel file, why do you need to create a table? Instead, you can organize your data into a cell array and send that into xlswrite().
% fake data
x = (1:10)';
y = rand(10,12);
dt = datetime(1979,3,3:14);
dtStr = [{''}, cellstr(datestr(dt))']; %convert dates to strings, first one empty.
data = [dtStr; [num2cell(x), num2cell(y)]]; % here's your full cell array with headers
xlswrite('mydata.xlsx', data)
For completeness here's how you would organize those data into a table.
% no empties allowed in headers; headers must start with non-numeric character
dtStr = [{'x'}, cellstr(datestr(dt, 'mmmm_dd_yy'))'];
data = [num2cell(x), num2cell(y)]; % here's your cell array without headers
dataTable = array2table(data, 'VariableNames', dtStr);
  댓글 수: 2
mabinj
mabinj 2019년 3월 14일
This works perfectly!
Thank you for your help :)
Adam Danz
Adam Danz 2019년 3월 14일
Glad I could help out!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by