Datetime conversion to table headings
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!