필터 지우기
필터 지우기

Problem in finding correct date.

조회 수: 1 (최근 30일)
Hrishikesh Das
Hrishikesh Das 2020년 4월 25일
답변: Kulko Margarita 2021년 2월 12일
I am writting a code to see whether a particular date is valid or not. The function takes three positive integer scalar inputs. If it is a valid date it returns true or else false.
When I try to use the following code in the command window
valid = valid_date(2018,4,1)
valid = valid_date(2018,4,31)
It returns false for both the cases. However it should return true for the first case and false for the second case.
My code is as follows
function valid = valid_date(year,month,day)
if ~isscalar(year) ||~isscalar(month) || ~isscalar(day);
valid = false;
else
valid = true;
end
if year<1 || month<1 || day<1;
valid = false;
else
valid = true;
end
if month==4||month==6||month==9||month==11 && day<=30
valid = true;
else
valid = false;
end
if month==1||month==3||month==5||month==7||month==8||month==10||month==12 && day<=31
valid = true;
else
valid = false;
end
  댓글 수: 1
Om Yadav
Om Yadav 2020년 4월 25일
Your if else is creating problem. You assigning 0 or 1 in each if condition. Ultimately, only last assigment survives. Let me give you an example of this. You just put your month==4 condition in the end and you will get the answer correct for months 4,6,9,11 and incorrect for others.
Suggestion is, only give condition and assignment. And else in the last loop like
function valid = valid_date(year,month,day)
if ~isscalar(year) ||~isscalar(month) || ~isscalar(day)
valid = false;
end
if year<1 || month<1 || day<1
valid = false;
end
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) && day<=31
valid = true;
end
if (month==4||month==6||month==9||month==11) && day<=30
valid = true;
else
valid=false;
end
But again Feb is problem so I am highlighting where you are faulting.

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

답변 (2개)

Ameer Hamza
Ameer Hamza 2020년 4월 25일
편집: Ameer Hamza 2020년 4월 25일
Try this simpler version
function tf = valid_date(y, m, d)
[Y,M,D] = ymd(datetime(y, m, d));
tf = all([Y M D] == [y m d]);
end
Result
>> valid_date(2018,4,1)
ans =
logical
1
>> valid_date(2018,4,31)
ans =
logical
0

Kulko Margarita
Kulko Margarita 2021년 2월 12일
function isvalid = valid_date(y, m, d)
% Check if the inputs are valid
% Check that they are scalars
if ~(isscalar(y) && isscalar(m) && isscalar(d))
isvalid = false;
% Check that inputs are positive
elseif ~all([y, m, d] > 0)
isvalid = false;
% Check that inputs are integers (not the data type)
elseif any(rem([y, m, d], 1))
isvalid = false;
% Check that m and d are below the max possible
elseif (m > 12) || (d > 31)
isvalid = false;
% The inputs could be a valid date, let's see if they actually are
else
% Vector of the number of days for each month
daysInMonth = [31 28 31 30 31 30 31 31 30 31 30 31];
% If leap year, change days in Feb
if isequal(rem(y, 4), 0) && (~isequal(rem(y, 100), 0) || isequal(rem(y, 400), 0))
daysInMonth(2) = 29;
end
maxDay = daysInMonth(m);
if d > maxDay
isvalid = false;
else
isvalid = true;
end
end
end

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by