Converting dates in time series
조회 수: 2 (최근 30일)
이전 댓글 표시
Dear All, I have a time seris that containts the time vector as :
27-Feb-2007 12:00:00 58.199 -136.64 -0.0048352
28-Feb-2007 12:00:00 58.199 -136.64 -0.0048265
01-Mar-2007 12:00:00 58.199 -136.64 -0.0048853
02-Mar-2007 12:00:00 58.199 -136.64 -0.0050604
03-Mar-2007 12:00:00 58.199 -136.64 -0.0051769
04-Mar-2007 12:00:00 58.199 -136.64 -0.0052122
I need to change the time format of the time column (first column) to decimal year format. I need the time format something like 2007.0123. please help me
댓글 수: 0
답변 (3개)
Davide Masiello
2022년 3월 7일
편집: Davide Masiello
2022년 3월 7일
Take a look at this
date = '27-Feb-2007 12:00:00';
str = datestr(datenum(date),'yyyy.dd.mm')
str =
'2007.27.02'
And for more info you can take a look at the following webpages
댓글 수: 0
Star Strider
2022년 3월 7일
편집: Star Strider
2022년 3월 7일
One approach —
M = {'27-Feb-2007 12:00:00' 58.199 -136.64 -0.0048352
'28-Feb-2007 12:00:00' 58.199 -136.64 -0.0048265
'01-Mar-2007 12:00:00' 58.199 -136.64 -0.0048853
'02-Mar-2007 12:00:00' 58.199 -136.64 -0.0050604
'03-Mar-2007 12:00:00' 58.199 -136.64 -0.0051769
'04-Mar-2007 12:00:00' 58.199 -136.64 -0.0052122};
format long
T1 = cell2table(M)
ddoy = day(datetime(T1.M1),'dayofyear')/day(datetime(year(T1.M1),12,31),'dayofyear');
% T1.M1 = year(datetime(T1.M1)) + ddoy(:,1) % Not Rounded
T1.M1 = year(datetime(T1.M1)) + round(ddoy(:,1), 4) % Round To 4 Decimal Places
I hope that there are easier ways to do this using datetime functions. I looked, however there doesn’t appear to be a format option for this.
EDIT — (7 Mar 2022 at 14:20)
Added round call to ‘ddoy’ display.
.
댓글 수: 2
Star Strider
2022년 3월 8일
The datetime function (and related functions) accounts for leap years, leap seconds, and every other known time variant.
Peter Perkins
2022년 3월 7일
Here's my solution:
>> tt = timetable(rand(6,1),rand(6,1),'RowTimes',datetime(2007,2,27:32,12,0,0))
tt =
6×2 timetable
Time Var1 Var2
____________________ _______ _______
27-Feb-2007 12:00:00 0.14929 0.92926
28-Feb-2007 12:00:00 0.25751 0.34998
01-Mar-2007 12:00:00 0.84072 0.1966
02-Mar-2007 12:00:00 0.25428 0.25108
03-Mar-2007 12:00:00 0.81428 0.61604
04-Mar-2007 12:00:00 0.24352 0.47329
>> lengthOfYear = @(t) caldays(between(dateshift(t,'start','year','current'),dateshift(t,'start','year','next'),'days'))
lengthOfYear =
function_handle with value:
@(t)caldays(between(dateshift(t,'start','year','current'),dateshift(t,'start','year','next'),'days'))
>>
>> tt.DecimalYear = year(tt.Time) + day(tt.Time,'dayofyear')./lengthOfYear(tt.Time)
tt =
6×3 timetable
Time Var1 Var2 DecimalYear
____________________ _______ _______ ___________
27-Feb-2007 12:00:00 0.14929 0.92926 2007.2
28-Feb-2007 12:00:00 0.25751 0.34998 2007.2
01-Mar-2007 12:00:00 0.84072 0.1966 2007.2
02-Mar-2007 12:00:00 0.25428 0.25108 2007.2
03-Mar-2007 12:00:00 0.81428 0.61604 2007.2
04-Mar-2007 12:00:00 0.24352 0.47329 2007.2
SS, I think this is just about what you did, except for putting the length of year calculation in a function.
Chris, unless you need to export these values, I'd think twice about creating them. It's kind of a terrible time representation in which you get poor precision, and even whole days are subject to round-off errors. SS, that kind of answers why there is not a simpler way to do this, but maybe this is a more common format that I'm aware of.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!