Clear Filters
Clear Filters

Datetime conversion to table headings

4 views (last 30 days)
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?

Accepted Answer

Adam Danz
Adam Danz on 14 Mar 2019
Edited: Adam Danz on 14 Mar 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 Comments
mabinj
mabinj on 14 Mar 2019
This works perfectly!
Thank you for your help :)

Sign in to comment.

More Answers (0)

Categories

Find more on Data Export to MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!