Main Content

Preserve Variable Names in Generated Code

If code readability is more important than reduced memory usage, specify that you want the code generator to preserve your variable names rather than reuse them in the generated code.

By default, when possible, variables share names and memory in the generated code. 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;
   ... 
 }

When the code generator preserves your variable names, the generated code can look like this code:

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

To specify that you want the code generator to preserve your variable names:

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

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

Preservation of variable names does not prevent an optimization from removing them from the generated code or prevent the C/C++ compiler from reusing them in the generated binary code.

Related Topics