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

1 vue (au cours des 30 derniers jours)
aakash re
aakash re le 13 Mai 2020
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 commentaire
John D'Errico
John D'Errico le 13 Mai 2020
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.

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 13 Mai 2020
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.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by