Main Content

Code Generation for Duration Arrays

The values in a duration array represent elapsed times in units of fixed length, such as hours, minutes, and seconds. You can create elapsed times in terms of fixed-length (24-hour) days and fixed-length (365.2425-day) years.

You can add, subtract, sort, compare, concatenate, and plot duration arrays.

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

Define Duration Arrays for Code Generation

For code generation, use the duration function to create duration arrays. For example, suppose the input arguments to your MATLAB® function are three numeric arrays of arbitrary size whose elements specify lengths of time as hours, minutes, and seconds. You can create a duration array from these three input arrays.

function d = foo(h,m,s) %#codegen
    d = duration(h,m,s);
end

You can use the years, days, hours, minutes, seconds, and milliseconds functions to create duration arrays in units of years, days, hours, minutes, or seconds. For example, you can create an array of hours from an input numeric array.

function d = foo(h) %#codegen
    d = hours(h);
end

Allowed Operations on Duration Arrays

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

OperationExampleNotes

assignment operator: =

d = duration(1:3,0,0);
d(1) = hours(5);

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

  • Delete an element.

  • Expand the size of a duration array.

relational operators: < > <= >= == ~=

d = duration(1:3,0,0);
tf = d(1) < d(2);

Code generation supports relational operators.

indexing operation

d = duration(1:3,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 = duration(1:3,0,0);
d2 = duration(4,30,0);
d = [d1 d2];

Code generation supports concatenation of duration arrays.

MATLAB Toolbox Functions That Support Duration Arrays

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

Related Topics