matlab.datetime.compatibility.convertDatenum
Syntax
Description
Use this function in code where you intend to perform calculations on
datetime values but, to preserve compatibility, need to interpret text
timestamps in the same way that datenum interprets them. This function is
designed to be a compatibility layer for use by customers who write functions.
[
additionally returns a logical value that is dt,wasDatenum] = matlab.datetime.compatibility.convertDatenum(d)1 (true)
if d is an array of serial date numbers or text timestamps, and
0 (false) if d is a
datetime array.
Examples
Input Arguments
Output Arguments
Tips
To enable code that works with
datetimearrays to accept text timestamps or serial date numbers as inputs in a backward-compatible way, add a call tomatlab.datetime.compatibility.convertDatenumat the beginning of your code.For example, if you have defined a function
myFuncthat accepts two input arguments, and the second input argument can be an array of text timestamps, serial date numbers, ordatetimevalues, process it by usingmatlab.datetime.compatibility.convertDatenum. Leave the rest of your code unchanged.function y = myFunc(A,D) D = matlab.datetime.compatibility.convertDatenum(D); <line 1 of original code> <line 2 of original code> ...In this example,
matlab.datetime.compatibility.convertDatenumoverwrites the input argumentD. IfDis already adatetimearray, then it is unaltered.To return an output that matches the data type of the input argument, use the second output of
matlab.datetime.compatibility.convertDatenum.For example, if you have defined a function
myFuncthat calls thedateshiftfunction, then you must usedatetimevalues in that calculation. But if the input tomyFunccould be an array of serial date numbers, then you can use the value ofwasDatenumto decide ifmyFuncreturns an array of serial date numbers or adatetimearray.To convert a
datetimearray to an array of serial date numbers, use theconvertTofunction.function DT = myFunc(D) [D,wasDatenum] = matlab.datetime.compatibility.convertDatenum(D); DT = dateshift(D,"end","month"); if wasDatenum DT = convertTo(DT,"datenum") end end
Version History
Introduced in R2022a