function not working properly

조회 수: 12 (최근 30일)
ba sa
ba sa 2021년 12월 4일
답변: Walter Roberson 2021년 12월 4일
i have this function, which is supposed to get three numbers and check if they're scalars and bigger than one
function valid = valid_date (year,month,day)
if ~isscalar(year)||year<1
valid=~1;
elseif ~isscalar(month)||month<1||month>12
valid=~1;
elseif ~isscalar(day)||day<1
valid=~1;
end
but when i give it
valid_date(2019,4,0)
or
valid_date(1,1,0)
it should give a logical 0 but it gives a 1
ans =
logical
1
it works correctly with (0,1,1) or (0,1,0) or (1,0,1) but this specific combination seems to break it
how do i fix it?

답변 (1개)

Walter Roberson
Walter Roberson 2021년 12월 4일
The code you post only assigns to valid in situations where it has valid=~1 so it can only ever assign false (0) to valid. There is no case in which the code can return 1 for valid.

카테고리

Help CenterFile Exchange에서 Linearization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by