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

1 Ansicht (letzte 30 Tage)
aakash re
aakash re am 13 Mai 2020
Beantwortet: Image Analyst am 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 Kommentar
John D'Errico
John D'Errico am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 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.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by