File Exchange

image thumbnail

Isleap

version 2.0.0.0 (18.6 KB) by

Calculate leap year

4 Downloads

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

Answer is: 0

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

Answer is:
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

Comments and Ratings (9)

yes it is possible. I'll change to make it compatible

Hi Giuseppe, do you have the Financial Toolbox installed? I get the error 'Undefined function 'year' for input arguments of type 'double'', if I run year(now). 'now' returns a date number, whereas 'year' (year number) requires a datetime value. 'year' (year of date), from the Financial Toolbox, accepts date numbers. Possibly this is the issue?

No, it is not an error.
help now
now Current date and time as date number.
T = now returns the current date and time as a serial date
number.

FLOOR(now) is the current date and REM(now,1) is the current time.
DATESTR(now) is the current date and time as a character vector.

See also date, datenum, datestr, clock.

I think there's a typo in the March 2018 update. Line 35 should be: year(datetime('now')), rather than year(now). Otherwise, this is very useful. Cheers!

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.

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

Marc FC

Updates

2.0.0.0

inputparser and github link

1.4.0.0

Changes in description

1.3.0.0

Modified after Simon Jan comment (also added into the help lines)

1.2.0.0

Change in description

1.1.0.0

Changes in help section

1.0.0.0

Improvement in input error handling

another logial indexing improvement

more elegant use of logical operators

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

MATLAB Online Live Editor Challenge

Win cash prizes and have your live script featured on our website

Learn more

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video