I would like this code to be converted to a function.
% Convert to function
year=input('Enter specified year(yyyy):');
if year>0
if mod(year,400)==0
leap_day=1;
else if mod(year,100)==0
leap_day=0;
else if mod(year,4)==0
leap_day=1;
else
leap_day=0;
end
end
end
end
month =input('Enter specified month(1-12):');
if month>=1 && month <=12
switch (month)
case {1,3,5,7,8,10,12}
max_day=31;
case {4,6,9,11}
max_day=30;
case {2}
max_day=28+leap_day;
end
fprintf('Enter specified day (1-%d):',max_day);
day=input('');
if day>=1 && day <=max_day
day_of_year=day;
for ii=1:month-1
switch(ii)
case {1,3,5,7,8,10,12}
day_of_year=day_of_year+31;
case {4,6,9,11}
day_of_year=day_of_year+30;
case {2}
day_of_year=day_of_year+28+leap_day;
end
end

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 10월 10일
편집: Andrei Bobrov 2017년 10월 10일

0 개 추천

>> calender1 = @(day1,month1,year1)datenum(year1,month1,day1) - datenum(year1 - 1,12,31);
>> calender1(3,1,2017)
ans =
3
>> calender1(3,3,2020)
ans =
63
>> calender1(3,3,2017)
ans =
62
>>
or create m - file: calcDaysOfYear.m
function num_of_day = calcDaysOfYear(day1,month1,year1)
dm = 30*ones(12,1);
x = rem(year1,[4,100,400]);
dm(2) = dm(2) - 1 - (x(:,1) | x(:,2)== 0 & x(:,3));
dm([1:2:7,8:2:end]) = dm([1:2:7,8:2:end]) + 1;
num_of_day = day1 + sum(dm(1:month1-1));
end
use
>>calcDaysOfYear(3,1,2017)
ans =
3
>>calcDaysOfYear(3,3,2016)
ans =
63
>>calcDaysOfYear(3,3,2017)
ans =
62

댓글 수: 1

jones matthew
jones matthew 2017년 10월 10일
Would you be able to make the code in terms of statements such as switch, else, else if, and if?

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

추가 답변 (1개)

Jan
Jan 2017년 10월 10일
편집: Jan 2017년 10월 10일

0 개 추천

If you really want to use this code instead of the built-in function of the Matlab toolbx (see Andrei's answer), start with:
function [day] = calender(day,month,year)
Now add the code and remove the fprintf and input lines.
Currently your code checks only if year > 0, but what should happen otherwise? Either add an else or define e.g. day=NaN as default values on top of the code.
Note that there is a function called "calender" already, so it is better to use a unique name.

댓글 수: 3

I currently have this code below. But I have errors on MATLAB. Would you be able to correct my code, and maybe look at the image I just posted? Thank you.
% code
function [day] = calender(day,month,year)
day=NaN;
if mod(year,400)==0
leap_day=1;
else if mod(year,100)==0
leap_day=0;
else if mod(year,4)==0
leap_day=1;
else
leap_day=0;
end
end
end
end
if month>=1 && month <=12
switch (month)
case {1,3,5,7,8,10,12}
max_day=31;
case {4,6,9,11}
max_day=30;
case {2}
max_day=28+leap_day;
end
day=input('');
if day>=1 && day <=max_day
day_of_year=day;
for ii=1:month-1
switch(ii)
case {1,3,5,7,8,10,12}
day_of_year=day_of_year+31;
case {4,6,9,11}
day_of_year=day_of_year+30;
case {2}
day_of_year=day_of_year+28+leap_day;
end
end
end
Jan
Jan 2017년 10월 10일
편집: Jan 2017년 10월 10일
There is no image. If you mention an error, please post a complete copy of it also. It is much easier to fix a problem, than ti guess, what the problem is. Thanks.
Start with:
function day_of_year = calender(day,month,year)
day_of_year = NaN;
to avoid overwriting the input "day".
Care for a proper indentation: Mark all code with Ctrl-A and press Ctrl-I.
It will be nicer to replace the "else if" by "elseif":
if mod(year,400)==0
leap_day=1;
elseif mod(year,100)==0
leap_day=0;
elseif mod(year,4)==0
leap_day=1;
else
leap_day=0;
end
Or shorter:
leap_day = (mod(year,4) == 0 && mod(year,100) ~= 0) || ...
(mod(year,400) == 0);
Remove the "day=input('');" also.
jones matthew
jones matthew 2017년 10월 10일
Thank you so much!

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

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2017년 10월 10일

편집:

2017년 10월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by