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.

You can reuse (reassign) an input, output, or local variable
with different class, size, or complexity if the code generator can
unambiguously determine the properties of each occurrence of this
variable during C/C++ code generation. If so, MATLAB^{®} creates
separate uniquely named local variables in the generated code. You
can view these renamed variables in the code generation report (see MATLAB Code Variables in a Report).

A common example of variable reuse is in `if-elseif-else`

or `switch-case`

statements.
For example, the following function `example1`

first
uses the variable * t* in an

`if`

statement,
where it holds a scalar double, then reuses `if`

statement to hold a vector of doubles.function y = example1(u) %#codegen if all(all(u>0)) % First, t is used to hold a scalar double value t = mean(mean(u)) / numel(u); u = u - t; end % t is reused to hold a vector of doubles t = find(u > 0); y = sum(u(t(2:end-1)));

You cannot reuse (reassign) variables if it is not possible to determine the class, size, and complexity of an occurrence of a variable unambiguously during code generation. In this case, variables cannot be renamed and a compilation error occurs.

For example, the following `example2`

function
assigns a fixed-point value to * x* in the

`if`

statement
and reuses `else`

clause.
It then uses `if-else`

statement.
This function generates a compilation error because after the `if-else`

statement,
variable `if-else`

clause executes.function y = example2(use_fixpoint, data) %#codegen if use_fixpoint % x is fixed-point x = fi(data, 1, 12, 3); else % x is a matrix of doubles x = data; end % When x is reused here, it is not possible to determine its % class, size, and complexity t = sum(sum(x)); y = t > 0; end

**Variable Reuse in an if Statement**

To see how MATLAB renames a reused variable * t*:

Create a MATLAB file

`example1.m`

containing the following code.function y = example1(u) %#codegen if all(all(u>0)) % First, t is used to hold a scalar double value t = mean(mean(u)) / numel(u); u = u - t; end % t is reused to hold a vector of doubles t = find(u > 0); y = sum(u(t(2:end-1))); end

Compile

`example1`

.For example, to generate a MEX function, enter:

codegen -o example1x -report example1.m -args {ones(5,5)}

**Note:**`codegen`

requires a MATLAB Coder™ license.When the compilation is complete,

`codegen`

generates a MEX function,`example1x`

in the current folder, and provides a link to the code generation report.Open the code generation report.

In the MATLAB code pane of the code generation report, place your pointer over the variable

inside the*t*`if`

statement.The code generation report highlights both instances of

in the*t*`if`

statement because they share the same class, size, and complexity. It displays the data type information forat this point in the code. Here,*t*is a scalar double.*t*In the MATLAB code pane of the report, place your pointer over the variable

outside the for-loop.*t*This time, the report highlights both instances of

outside the*t*`if`

statement. The report indicates thatmight hold up to*t*`25`

doubles. The size ofis*t*`:25`

, that is, a column vector containing a maximum of`25`

doubles.Click the

**Variables**tab to view the list of variables used in`example1`

.The report displays a list of the variables in

`example1`

. There are two uniquely named local variablesand*t>1*.*t>2*In the list of variables, place your pointer over

.*t>1*The code generation report highlights both instances of

in the*t*`if`

statement.In the list of variables, place your pointer over

*t>2*The code generation report highlights both instances of

outside the*t*`if`

statement.

The following variables cannot be renamed in generated code:

Persistent variables.

Global variables.

Variables passed to C code using

`coder.ref`

,`coder.rref`

,`coder.wref`

.Variables whose size is set using

`coder.varsize`

.Variables whose names are controlled using

`coder.cstructname`

.The index variable of a

`for`

-loop when it is used inside the loop body.The block outputs of a MATLAB Function block in a Simulink

^{®}model.Chart-owned variables of a MATLAB function in a Stateflow

^{®}chart.

Was this topic helpful?