Round, Ceil and Floor Matlab-datenumbers

round/ceil/floor Matlab datenumbers to the nearest N or multiple N of a time_unit
454 Downloads
Updated 25 Mar 2014

View License

datenumRound2_OH rounds a Matlab datenumber to the nearest N or multiple N of a time_unit ('YY','MO','DD','DOY','DOW','HH','MM','SS').
datenumCeil2_OH and datenumFloor2_OH ceil and floor a Matlab datenumber to the nearest N or multiple N of a time_unit

Inspired by round2.m from the Matlab FEX.

INPUT:
datenumber : Matlab datenumber
time_unit : 'YY','MO','DD','DOY','DOW','HH','MM' or 'SS'
N : (multiple of) time_unit to round to (default: 1)
do_Nmult : 0/1; 0 --> round to N (like round.m) (default: 0)
1 --> round to multiple of N (like round2.m)
do_one : 0/1/2; 1/2 --> for time_units that count from 1 instead of 0
(i.e. 'DD',DOY' and 'MO') take N as incremental summation
starting at 1. Is only used in combination with do_Nmult=1.
1_vs_2; --> include last day of the month (1) or not (2)
eg 'DD', N=5: DD=30,5,10,15,20,25,30,1,.. for do_one=0
DD= 1,6,11,16,21,26,31,1,.. for do_one=1
(default: 0) DD= 1,6,11,16,21,26, 1,6,.. for do_one=2

OUTPUT:
datenumber : rounded Matlab datenumber

EXAMPLES:
datestr(datenumRound2_OH(datenum(2012,1,28,22,30,0),'HH',2,1))
--> 28-Jan-2012 22:00:00
datestr(datenumRound2_OH(datenum(2012,1,28,22,30,0),'HH',2,0))
--> 29-Jan-2012 02:00:00

Cite As

Oscar Hartogensis (2024). Round, Ceil and Floor Matlab-datenumbers (https://www.mathworks.com/matlabcentral/fileexchange/38167-round-ceil-and-floor-matlab-datenumbers), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Dates and Time in Help Center and MATLAB Answers
Acknowledgements

Inspired by: round2

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.4.0.0

For very long datenumber-records datenumRound2_OH would give an "out of memory" error. This had to do with the unnecessary usage of diag.m, which is now replaced with a more memory friendly solution.

1.3.0.0

* Added do_one option: for time_units that count from 1 instead of 0 (i.e. 'DD',DOY' and 'MO') take N as incremental summation starting at 1
* Some small bug-fixes

1.2.0.0

solved a rounding error in intermediate time_variables that caused e.g. datestr(datenumFloor2_OH(datenum(2012,1,28,16,0,0),'HH',2,1)) to give 28-Jan-2012 14:00:00 instead of 16:00

1.1.0.0

Added acknowledgement to round2

1.0.0.0