How can I convert time from seconds to decimal year in Matlab?

16 ビュー (過去 30 日間)
Yoni Verhaegen -WE-1718-
Yoni Verhaegen -WE-1718- 2019 年 10 月 25 日
コメント済み: Steven Lord 2019 年 10 月 25 日
I have a dataset which includes the seconds that have passed since 2000-01-01 00:00:00.0 and I would like them to be converted to decimal years (for example 2013.87).
Can anyone help me out on this? Thanks!
An example from the dataset:
416554767.293262
416554768.037637
416554768.782013
416554769.526386
416554770.270761
416554771.015136
416554771.759509
416554772.503884
416554773.248258
416554773.992632
416554774.737007
416554775.481381
416554776.225757
416554776.970131
416554777.714504
416554778.458880

回答 (1 件)

Guillaume
Guillaume 2019 年 10 月 25 日
First convert your numbers to datetime. Trivially done:
d = [416554767.293262
416554768.037637
416554768.782013
416554769.526386
416554770.270761
416554771.015136
416554771.759509
416554772.503884
416554773.248258
416554773.992632
416554774.737007
416554775.481381
416554776.225757
416554776.970131
416554777.714504
416554778.458880]; %demo data
dd = datetime(d, 'ConvertFrom', 'epochtime', 'Epoch', '2000-01-01') %optionally specify a Format for display
Personally, I'd leave it like that and use the datetime from here on. It's more likely to be more useful if you want to perform calculations on dates.
If your really want your fractionally year:
y = year(dd) + years(dd - dateshift(dd, 'start', 'year'))
The above extract the year part of the datetime, then adds the duration in years between the datetime and the same datetime shifted to the start of the year.
  1 件のコメント
Steven Lord
Steven Lord 2019 年 10 月 25 日
Another way to do this that results in almost the same answer and looks a bit closer to the English description of the task:
d = [416554767.293262
416554768.037637
416554768.782013
416554769.526386
416554770.270761
416554771.015136
416554771.759509
416554772.503884
416554773.248258
416554773.992632
416554774.737007
416554775.481381
416554776.225757
416554776.970131
416554777.714504
416554778.458880]; %demo data
dd2 = datetime('2000-01-01') + seconds(d);
seconds(dd - dd2) % Very small differences

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by