File Exchange

## Isleap

version 2.0 (17.6 KB) by

Calculate leap year

4.33333
3 Ratings

Updated

ISLEAP True for leap year.
ISLEAP(Year) returns 1 if Year is a leap year and 0 otherwise.
ISLEAP is only set for gregorian calendar, so Year >= 1583
Syntax: ISLEAP(YEAR)

Inputs:
YEAR - Year of interest (default = current year).
You can input a vector of years.
Outputs:
Logical vector.

Example:

Calling on Matlab the function: isleap

Calling on Matlab the function: x=isleap([2007 2008])

x = 0 1

Created by Giuseppe Cardillo
giuseppe.cardillo-edta@poste.it
Modified after Simon Jan suggestions
To cite this file, this would be an appropriate format:
Cardillo G. (2007) Isleap: a simple routine to test if a year is leap.
http://www.mathworks.com/matlabcentral/fileexchange/14172

Jan

### Jan (view profile)

Descriptive help, reliable error checking, new version is really efficient, does exactly what the name says.

Jan

### Jan (view profile)

CLOCK -> DATE -> DATESTR -> STR2DOUBLE waste a lot of time: str2double(datestr(date,10))
Faster: c = clock; default = {c(1)};

Checking the number of inputs can be done easier and faster without VARARGIN, e.g.:
function x = isleap(Year)
switch nargin
case 1, % Your error checks...
case 0, c = clock; Year = c(1);
otherwise error(...);
end

The permanent disabling of the warning for logical conversion is not secure. You can omit this by simplifying the engine - instead of:
warning('OFF','MATLAB:conversionToLogical')
A=logical(mod(Year,4));
B=logical(mod(Year,100));
C=logical(mod(Year,400));
x=ismember(Year,Year(~C | (~A & B)));
call just:
x = ~mod(Year, 4) & (mod(Year, 100) | ~mod(Year, 400));
And suddenly the function is 150 times faster (400 times if called without argument).
Good help, H1-line, date, author and reference included. I'll rate this, if its efficiency is encreased.

Vince Petaccio

### Vince Petaccio (view profile)

Cool, just what I needed. Thanks! :o]

Marc FC