Converting dates in time series
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (3)
Davide Masiello
am 7 Mär. 2022
Bearbeitet: Davide Masiello
am 7 Mär. 2022
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 Kommentare
Star Strider
am 7 Mär. 2022
Bearbeitet: Star Strider
am 7 Mär. 2022
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 Kommentare
Star Strider
am 8 Mär. 2022
The datetime function (and related functions) accounts for leap years, leap seconds, and every other known time variant.
Peter Perkins
am 7 Mär. 2022
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 Kommentare
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!