Reuse the Same Variable with Different Properties

When You Can Reuse the Same Variable with Different Properties

You can reuse (reassign) an input, output, or local variable with different class, size, or complexity if MATLAB® 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 Viewing Variables in Your MATLAB Code).

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 t outside the 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)));

To compile this example and see how MATLAB renames the reused variable t, see Variable Reuse in an if Statement.

When You Cannot Reuse Variables

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 x to store a matrix of doubles in the else clause. It then uses x after the if-else statement. This function generates a compilation error because after the if-else statement, variable x can have different properties depending on which 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:

  1. 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
  2. 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.

  3. Open the code generation report.

  4. In the MATLAB code pane of the code generation report, place your pointer over the variable t inside the if statement.

    The code generation report highlights both instances of t in the if statement because they share the same class, size, and complexity. It displays the data type information for t at this point in the code. Here, t is a scalar double.

  5. In the MATLAB code pane of the report, place your pointer over the variable t outside the for-loop.

    This time, the report highlights both instances of t outside the if statement. The report indicates that t might hold up to 25 doubles. The size of t is :25, that is, a column vector containing a maximum of 25 doubles.

  6. 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 variables t>1 and t>2.

  7. In the list of variables, place your pointer over t>1.

    The code generation report highlights both instances of t in the if statement.

  8. In the list of variables, place your pointer over t>2

    The code generation report highlights both instances of t outside the if statement.

Limitations of Variable Reuse

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?