Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To convert MATLAB^{®} code to efficient C/C++ code, the code
generator introduces optimizations that intentionally cause the generated
code to behave differently, and sometimes produce different results,
than the original source code.

Here are some of the differences:

MATLAB supports 16-bit characters, but the generated code represents characters in 8 bits, the standard size for most embedded languages like C. See Code Generation for Character Arrays.

Generated code does not enforce order of evaluation in expressions. For most expressions, order of evaluation is not significant. However, for expressions with side effects, the generated code may produce the side effects in different order from the original MATLAB code. Expressions that produce side effects include those that:

Modify persistent or global variables

Display data to the screen

Write data to files

Modify the properties of handle class objects

In addition, the generated code does not enforce order of evaluation of logical operators that do not short circuit.

For more predictable results, it is good coding practice to split expressions that depend on the order of evaluation into multiple statements.

Rewrite

A = f1() + f2();

as

A = f1(); A = A + f2();

so that the generated code calls

`f1`

before`f2`

.Assign the outputs of a multi-output function call to variables that do not depend on one another. For example, rewrite

[y, y.f, y.g] = foo;

as

[y, a, b] = foo; y.f = a; y.g = b;

When you access the contents of multiple cells of a cell array, assign the results to variables that do not depend on one another. For example, rewrite

[y, y.f, y.g] = z{:};

as

[y, a, b] = z{:}; y.f = a; y.g = b;

Generated code does not match the termination behavior of MATLAB source code. For example, if infinite loops do not have side effects, optimizations remove them from generated code. As a result, the generated code can possibly terminate even though the corresponding MATLAB code does not.

For variable-size N-D arrays, the `size`

function
might return a different result in generated code than in MATLAB source
code. The `size`

function sometimes
returns trailing ones (singleton dimensions) in generated code, but
always drops trailing ones in MATLAB.
For example, for an N-D array `X`

with dimensions ```
[4
2 1 1]
```

, `size(X)`

might return ```
[4
2 1 1]
```

in generated code, but always
returns `[4 2]`

in MATLAB. See Incompatibility with MATLAB in Determining Size of Variable-Size
N-D Arrays.

The size of an empty array in generated code might be different from its size in MATLAB source code. See Incompatibility with MATLAB in Determining Size of Empty Arrays.

Deleting all elements of an array results in an empty array. The size of this empty array in generated code might differ from its size in MATLAB source code.

Case | Example Code | Size of Empty Array in MATLAB | Size of Empty Array in Generated Code |
---|---|---|---|

Delete all elements of an m-by-n
array by using the `colon` operator (`:` ). | ```
coder.varsize('X',[4,4],[1,1]);
X = zeros(2);
X(:) = [];
``` | `0-by-0` | `1-by-0` |

Delete all elements of a row
vector by using the `colon` operator (`:` ). | ```
coder.varsize('X',[1,4],[0,1]);
X = zeros(1,4);
X(:) = [];
``` | `0-by-0` | `1-by-0` |

Delete all elements of a column
vector by using the `colon` operator (`:` ). | ```
coder.varsize('X',[4,1],[1,0]);
X = zeros(4,1);
X(:) = [];
``` | `0-by-0` | `0-by-1` |

Delete all elements of a column vector by deleting one element at a time. | coder.varsize('X',[4,1],[1,0]); X = zeros(4,1); for i = 1:4 X(1)= []; end | `1-by-0` | `0-by-1` |

The generated code might not produce the same floating-point numerical results as MATLAB in the following situations:

When computer hardware uses extended precision registers

The generated code might not produce exactly the same pattern
of `NaN`

and `inf`

values as MATLAB code
when these values are mathematically meaningless. For example, if MATLAB output
contains a `NaN`

, output from the generated code
should also contain a `NaN`

, but not necessarily
in the same place.

The `coder.target`

function returns different
values in MATLAB than in the generated code. The intent is to
help you determine whether your function is executing in MATLAB or
has been compiled for a simulation or code generation target. See `coder.target`

.

Before code generation, at class loading time, MATLAB computes class initial values. The code generator uses the value that MATLAB computes. It does not recompute the initial value. If the initialization uses a function call to compute the initial value, the code generator does not execute this function. If the function modifies a global state, for example, a persistent variable, code generator might provide a different initial value than MATLAB. For more information, see Defining Class Properties for Code Generation.

See Incompatibilities with MATLAB in Variable-Size Support for Code Generation.

Was this topic helpful?