Main Content

Reuse Large Arrays and Structures

Variable reuse can reduce memory usage or improve execution speed, especially when your code has large structures or arrays. However, variable reuse results in less readable code. If reduced memory usage is more important than code readability, specify that you want the code generator to reuse your variables in the generated code.

The code generator can reuse the name and memory of one variable for another variable when:

  • Both variables have the same memory requirements.

  • Memory access for one variable does not interfere with memory access for the other variable.

The code generator reuses your variable names for other variables or reuses other variable names for your variables. For example, for code such as:

if (s>0) 
    myvar1 = 0; 
    ... 
else 
    myvar2 = 0; 
    ... 
end 

the generated code can look like this code:

 if (s > 0.0) {
   myvar2 = 0.0;
    ...
 } else {
   myvar2 = 0.0;
   ... 
 }

To specify that you want the code generator to reuse your variables:

  • In a code generation configuration object, set the PreserveVariableNames parameter to 'None'.

  • In the MATLAB® Coder™ app, set Preserve variable names to None.

Related Topics