why does my function keep showing logical 0 for every input?

1 回表示 (過去 30 日間)
aakash re
aakash re 2020 年 5 月 13 日
回答済み: Image Analyst 2020 年 5 月 13 日
function valid= valid_date(date,month,year)
if isscalar(date) && date>0 && month>0 && year>0 && isscalar(year) && isscalar(month) && date==fix(date) && month==fix(month) && year==fix(year)&& month<12 && date<31
if mod(year,4)==0 || mod(year,400)==0 && mod(year,100)~=0
if month==2
if date<=29
valid= true;
else
valid=false;
end
elseif month==1|| month==3|| month==5|| month==7|| month==8 || month==10 month==12
if date<=31
valid=true;
else
valid=false;
end
elseif month==4|| month==6|| month==9|| month==11
if date<=30
valid=true;
else
valid=false;
end
end
else
if month==2
if date<=28
valid= true;
else
valid=false;
end
elseif month==1|| month==3|| month==5|| month==7|| month==8 || month==10 month==12
if date<=31
valid=true;
else
valid=false;
end
elseif month==4|| month==6|| month==9|| month==11
if date<=30
valid=true;
else
valid=false;
end
end
end
else
valid=false;
end
end
  1 件のコメント
John D'Errico
John D'Errico 2020 年 5 月 13 日
You don't actually tell what inputs you might be passing to this function. I might bet that has something significant to do with your problem.

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

回答 (1 件)

Image Analyst
Image Analyst 2020 年 5 月 13 日
You're missing a || in two places, like:
elseif month==1|| month==3|| month==5|| month==7|| month==8 || month==10 month==12
^^
Plus, don't call your variables month, date, and year since those are built-in functions names.

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by