Main Content

Code Generation for Datetime Arrays

The values in a datetime array represent points in time using the proleptic ISO calendar.

When you use datetime arrays with code generation, adhere to these restrictions.

Define Datetime Arrays for Code Generation

For code generation, use the datetime function to create datetime arrays. For example, suppose the input arguments to your MATLAB® function are numeric arrays whose values indicate the year, month, day, hour, minute, and second components for a point in time. You can create a datetime array from these input arrays.

function d = foo(y,mo,d,h,mi,s) %#codegen
    d = datetime(y,mo,d,h,mi,s);
end

Allowed Operations on Datetime Arrays

For code generation, you are restricted to the operations on datetime arrays listed in this table.

OperationExampleNotes

Assignment operator: =

d = datetime(2019,1:12,1,12,0,0);
d(1) = datetime(2019,1,31);

Code generation does not support using the assignment operator = to:

  • Delete an element.

  • Expand the size of a datetime array.

Relational operators: < > <= >= == ~=

d = datetime(2019,1:12,1,12,0,0);
tf = d(1) < d(2);

Code generation supports relational operators.

Indexing operation

d = datetime(2019,1:12,1,12,0,0);
idx = [1 2];
d(idx);
idx = logical([1 1 0]);
d(idx);

Code generation supports indexing by position, linear indexing, and logical indexing.

Concatenation

d1 = datetime(2019,1:6,1,12,0,0);
d2 = datetime(2019,7:12,1,12,0,0);
d = [d1 d2];

Code generation supports concatenation of datetime arrays.

MATLAB Toolbox Functions That Support Datetime Arrays

For code generation, you can use datetime arrays with these MATLAB toolbox functions:

Related Topics