Converting Day of Year to month and day

조회 수: 82 (최근 30일)
Hamza
Hamza 2012년 9월 25일
댓글: michele paoletti 2019년 6월 14일
Dear All
I have the following data:
epoch= '96318.74847837'
'96319.62211352'
'96319.62351606'
'96319.62356237'
'96320.05952563'
'96320.49676119'
the firs 2 elements of the data is year '96' and rest is the day of year '318.74847837'. I got the following code to separate them.
year=cellfun(@(y) y(1:2),epoch,'uni',false);
day=cellfun(@(y) y(3:5),epoch,'uni',false);
And the following code to convert day of year to date and month.
doy=318;
dayspermonth = [0 31 28 31 30 31 30 31 31 30 31 30 31];
mm = ones(size(doy))*NaN;
dd = ones(size(doy))*NaN;
for im = 1:12
I = find(doy > sum(dayspermonth(1:im)) & doy <= sum(dayspermonth(1:im+1)));
mm(I) = ones(size(I)).*im;
dd(I) = doy(I) - ones(size(I))*sum(dayspermonth(1:im));
end
I would like to take the input of day into doy so that I have a single array of doy with all the results from day.
I know this code is not taking care of leap year. but if someone can help me with that I would be grateful.
Thanks!!
  댓글 수: 1
Star Strider
Star Strider 2012년 9월 25일
LEAP YEARS — To find and work with leap years, see the documentation for eomday. You can use it to create your dayspermonth vector for any given year, including correct values for leap years.

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 9월 25일
편집: Andrei Bobrov 2012년 9월 25일
% Let y - our year
y = 1996;
doy = 318.25;
[yy mm dd HH MM] = datevec(datenum(y,1,doy));
variant of full solution
x= {'96318.74847837'
'96319.62211352'
'96319.62351606'
'96319.62356237'
'96320.05952563'
'96320.49676119'} % initial array
a = str2double(x);
y = fix(a/1000);
[yy mm dd HH MM SS] = datevec(datenum(y,0,a - y*1000)); % corrected
  댓글 수: 4
Hamza
Hamza 2012년 9월 25일
Thanks Andrei! in the end, how to convert your output [yy mm dd HH MM SS] into dd-mm-yy HH:MM:SS format?
Hamza
Hamza 2012년 9월 25일
I tried
str= datestr(datenum([yy mm dd HH MM SS]),0)
and works good

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

추가 답변 (2개)

Jan
Jan 2012년 9월 25일
편집: Jan 2012년 9월 25일
When I understand you correctly, this is the question:
Convert "doy" to "day" and "month":
day = {'318.74847837', ...
'319.62211352', ...
'319.62351606', ...
'319.62356237', ...
'320.05952563', ...
'320.49676119'};
Solution:
daynum = sscanf(sprintf('%s*', day{:}), '%g*');
datev = datevec(daynum);
day = datev(:, 3); % [EDITED]
month = datev(:, 2); % [EDITED]
Now this does not consider the leap year. To do this:
daynum = sscanf(sprintf('%s*', day{:}), '%g*');
yearnum = sscanf(sprintf('%s*', year{:}), '%g*');
yearvec = zeros(numel(yearnum), 6);
yearvec(:, 1) = yearnum; % [EDITED], no transpose
yearvec(:, 1) = 1;
% Perhaps also: yearvec(:,3) = 1;
yearsn = datenum(yearvec);
datesn = yearsn + daynum;
datev = datevec(datesn);
day = datev(:, 3); % [EDITED]
month = datev(:, 2); % [EDITED]
  댓글 수: 6
Hamza
Hamza 2012년 9월 25일
Apologies for not clarifying. epoch is actually a cell string. I am trying to run your approach but it's not working.
It runs if i'm taking input from:
year=cellfun(@(y) y(1:2),epoch,'uni',false);
day=cellfun(@(y) y(3:5),epoch,'uni',false);
But still the output is just '1' for both. And as you said that it won't do good if i mix two different approaches. Any suggestions where I am going wrong?
Jan
Jan 2012년 9월 25일
편집: Jan 2012년 9월 25일
Yes, use CELLFUN to get "day" and "year", when you like to. Afterwards I had a bug in my code. Replace:
day = datev(3);
month = datev(2);
by:
day = datev(:, 3);
month = datev(:, 2);
See [EDITED]

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


michele paoletti
michele paoletti 2019년 6월 14일
I need convert from year and day of year to day and month. How can i do?
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2019년 6월 14일
Let:
year1 = 2019;
dayofyear = 241;
then:
out = datetime(2019,1,dayofyear)
michele paoletti
michele paoletti 2019년 6월 14일
Thank you so much!

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

카테고리

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