why does my function keep showing logical 0 for every input?
37 views (last 30 days)
Show older comments
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 Comment
John D'Errico
on 13 May 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.
Answers (1)
Image Analyst
on 13 May 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.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!