MATLAB: Applying LeapYear function into the Matlab Scipt

homeworkleapyearmatlab functiontrueorfalse

Creat a function called isLeapYear(intYear) that determines if the input year is a leap year or not. Write the main program to calculate the total days passed in a year for a given date. Prompt the user for the (integer) inputs: year, month, and day. The output is the total days passed since Jan 1st of that year excluding the input date (e.g., if the user gives 2012, 01, 23, then the output is 22). Call the function isLeapYear(intYear) to determine if the year is leap or not, if necessary.
Here's my function:
function LeapYear = isLeapYear(intYear)
a = mod(intYear,400);
b = mod(intYear,100);
c = mod(intYear,4);
LeapYear = ((a==0)||(b~=0 && c==0));
end
Here's my main code:
clear,clc
%1. Get Inputs
intYear = input('Enter Year: ');
intMonth = input('Enter Month: ');
intDay = input('Enter Day: ');
%2. Calculate days passed
if isLeapYear==0
intDaysPassed = ((((intMonth - 1)*30)+intDay) - 1);
else
strMessage = 'Please enter a valid leap year';
msgbox(strMessage)
return
end
I tried using "isLeapYear==0" but it's not working. How would I be able to define if a function is true or not in order to proceed with the process?
Thank you so much !!

Best Answer

Put your isLeapYear function code (everything from the function LeapYear etc line to the corresponding end line) in a separate file called isLeapYear.m and make sure this file is on the MATLAB path somewhere (or in your current directory) so MATLAB can find it when you call the function.
Change your main code call from
if isLeapYear == 0
to
if isLeapYear(intYear)
Modify your intDaysPassed formula to account for months that do not have exactly 30 days in them. You might consider starting with a 12 element array that has the number of days in each month, and then work out the logic from there.
Modify your if isLeapYear etc logic so that it prints out the days passed for all user inputs, not just the ones that happen to be leap years.