change date format in table

조회 수: 3 (최근 30일)
Sara Antonio
Sara Antonio 2017년 12월 28일
편집: Rena Berman 2024년 7월 16일
I have several txt files with date format set as dd/MM/uuuu hh:mm:ss (e.g 01/06/2008 00:15:00) that I read them into a table. When I convert them into timetable the date mantains in the same format. However when I add rows in the table in a loop, the date format changes into MM/dd/uuuu, and I get this warning: Warning: The DATETIME data was created using format 'MM/dd/uuuu HH:mm:ss' but also matched 'dd/MM/uuuu HH:mm:ss'. To avoid ambiguity, use a format character vector. e.g. '%{MM/dd/uuuu HH:mm:ss}D'
Where do I change the date format as it says in the e.g?
Here is my code:
%Initial empty table
T = timetable(datetime('01/Jan/2000 00:00:00'), NaN, NaN, NaN);
T.Properties.VariableNames = {'SignificantWaveHeight_Hsig_', 'SignificantWavePeriod_Tsig_', 'MeanMagneticDirection'};
for i = 8 %1:numel(allNames)
a = readtable(allNames{i});
c = a(:,{'Received', 'SignificantWaveHeight_Hsig_', 'SignificantWavePeriod_Tsig_', 'MeanMagneticDirection'});
b =table2timetable(c);
TT2 = retime(b,'daily','mean');
T = [T; TT2];
end

답변 (1개)

Peter Perkins
Peter Perkins 2017년 12월 28일
The warning is coming from readtable, and it's worth paying attention to what it says, and addressing the issue, because that is the root of your problem.
Also, when you concatenate two datetime vectors (which is what's happening when you concatenate T and TT2), the format of the result is the same as the format of the first vector in the concatenation. Look at the format you have used in initializing your timetable.
  댓글 수: 2
Sara Antonio
Sara Antonio 2017년 12월 28일
Thanks for your reply. How do I set the date format then when I use readtable? That is the part I can't do it. My final table instead of 1-jan-2008 to 30-jan-2008 is resulting in:
1-jan; 1-Feb; ... ; 1-Dec, 13-Jan; ... 30-Jan; 1-jan and so on.
How can I work this out because I cannot change the raw data.
Peter Perkins
Peter Perkins 2018년 1월 5일
편집: Rena Berman 2024년 7월 16일
See the description of the 'Format' parameter in the readtable documentation, which will lead you to the doc for textscan.
If you have a recent version of MATLAB, you can also use detectimportoptions .

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by