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

2 visualizaciones (últimos 30 días)
aakash re
aakash re el 13 de Mayo de 2020
Respondida: Image Analyst el 13 de Mayo de 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 comentario
John D'Errico
John D'Errico el 13 de Mayo de 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.

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 13 de Mayo de 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.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by