How to write a timetable to excel with rowtimes as dates without times?

조회 수: 12 (최근 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?

채택된 답변

Star Strider
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)
dates = 3×1 datetime array
15-Jan-2025 15-Feb-2025 15-Mar-2025
tt = timetable(dates,m);
writetimetable(tt,'tt.xlsx')
TT1 = readtimetable('tt.xlsx')
TT1 = 3×1 timetable
dates m ___________ _ 15-Jan-2025 1 15-Feb-2025 2 15-Mar-2025 3
(I am using Ubuntu 24.04 so I do not have Excel or access to it.)
.
  댓글 수: 12
Star Strider
Star Strider 2025년 7월 5일
As always, my pleasure!
If you are using them only in MATLAB, and do not need to use them in any other application, or share them with anyone not having access to MATLAB, consider saving them (save and load) to .mat files.
Jeremy Hughes
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
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')
ans = 3×1 timetable
dates m ___________ _ 15-Jan-2025 1 15-Feb-2025 2 15-Mar-2025 3
  댓글 수: 1
Dyuman Joshi
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
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
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);
Lars Svensson
Lars Svensson 2025년 7월 7일
Many thanks, Paul. I am afraid that I not read your answer thoroughly enough and missed the main point, the use of PreserveFormat and UseExcel .
However, when I try your suggestion on my Mac, it still does not work and I get the following error message
"Warning: Using Excel for reading and writing spreadsheet files is only supported on Windows. Switching 'UseExcel' to false."
So it works on Windows but not om Mac.

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


Walter Roberson
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
Stephen23
Stephen23 2025년 8월 11일
편집: Stephen23 2025년 8월 25일
"The documentation does not say that PreserveFormat is applicable only to Windows. Should it?"
Yes
Paul
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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by