Datetime conversion to table headings

3 Ansichten (letzte 30 Tage)
mabinj
mabinj am 14 Mär. 2019
Kommentiert: Adam Danz am 14 Mär. 2019
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?

Akzeptierte Antwort

Adam Danz
Adam Danz am 14 Mär. 2019
Bearbeitet: Adam Danz am 14 Mär. 2019
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 Kommentare
mabinj
mabinj am 14 Mär. 2019
This works perfectly!
Thank you for your help :)
Adam Danz
Adam Danz am 14 Mär. 2019
Glad I could help out!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by