How to write a timetable to excel with rowtimes as dates without times?
11 次查看(过去 30 天)
显示 更早的评论
Take this simple example:
m = (1:3)';
dates = datetime(2025,m,15);
tt = timetable(dates,m);
writetimetable(tt,'tt.xlsx')
tt is a 3x1 timetable with dates but no times 00:00:
dates m
___________ _
15-Jan-2025 1
15-Feb-2025 2
15-Mar-2025 3
But the resulting excel sheet tt.xlsx includes the times 00:00:
dates m
1/15/25 00:00 1
2/15/25 00:00 2
3/15/25 00:00 3
How can I make writetimetable create an excel sheet with dates but no times 00:00?
0 个评论
采纳的回答
Star Strider
2025-7-4
That is likely a problem with Excel.
MATLAB writes the timetable correctly --
m = (1:3)';
dates = datetime(2025,m,15)
tt = timetable(dates,m);
writetimetable(tt,'tt.xlsx')
TT1 = readtimetable('tt.xlsx')
(I am using Ubuntu 24.04 so I do not have Excel or access to it.)
.
12 个评论
Star Strider
2025-7-5
Jeremy Hughes
2025-8-11
I'll confirm that the format that Excel is applying here is out of MATLAB's control. The PreserveFormat argument should work on a Mac, but has no effect on the date format, unfortunately.
更多回答(3 个)
Chuguang Pan
2025-7-4
编辑:Chuguang Pan
2025-7-4
You can use "InputFormat" option to specify the date format
m = (1:3).';
dates = datetime(2025,m,15,"InputFormat","dd-MM-yyyy");
tt = timetable(dates,m);
writetimetable(tt,'TT.xlsx');
readtimetable('TT.xlsx')
1 个评论
Dyuman Joshi
2025-7-4
编辑:Dyuman Joshi
2025-7-4
This does not work. The problem is with the excel that saves the data - Open the excel and you'd find the issue OP is facing.
Paul
2025-7-5
"If there was a way in Matlab to change the format in the first column in the excel file from "m/d/yy hh:mm" to "d-mmm-yyyy", my problem would be solved."
Seems like you might be able to define an empty .xlsx file that has the first column, starting from the second row, have the format you want. Call it empty.xlsx. Then when you want to write, copyfile empty.xlsx to the filename you want, and then use writetimetable with PreserveFormat and UseExcel both set to true. I didn't test this approach.
3 个评论
Paul
2025-7-6
Interesting. Works for me. Windows 11. Matlab 2024a.
>> copyfile empty.xlsx tt.xlsx
>> m = (1:3)';
>> dates = datetime(2025,m,15);
>> tt = timetable(dates,m);
>> writetimetable(tt,'tt.xlsx','PreserveFormat',true,'UseExcel',true);

Walter Roberson
2025-7-7
For MacOS and Linux, it is not possible to PreserveFormat .
About the best you can do on MacOS is to convert the timetable to a table, set the Format property of the appropriate column of the table to something like 'dd-MMM-uuuu', then set the appropriate column to be string() of the appropriate column. This will convert the column to the text in dd-MMM-uuuu format.
Unfortunately it is likely that Excel will then interpret the column as text rater than as datetime format.
3 个评论
Paul
2025-8-25
According to @Jeremy Hughes in this comment, PreserveFormat should work on Mac, but perhaps with no capability to format dates.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!