Simulink Code Generation - Variable names changes of MATLAB function blocks in C++ generated code
4 views (last 30 days)
Good evening, I'm generating C++ code from a model in Simulink using the Embedded Coder toolbox, in which are present some MATLAB functions. In the generated code I saw that the variable names present in the Matlab function blocks of the model are changed with other variable names. For example the next are the lines of MATLAB code I used in one of the MATLAB function blocks:
function K_ec_bar = K_control(T, ec_bar_d, ec_bar, Ac_CT, Bc_CT, mu_K)
eig_des_K_control = exp(mu_K*T); % mu_K is a 2x1 vector
poly_K=conv([1 -eig_des_K_control(1)],[1 -eig_des_K_control(2)]);
The generated C++ code for this part is:
eig_des_K_control_idx_0 = std::exp(U.mu_K * U.T);
b2 = std::exp(U.mu_K * U.T);
Ac_DT = -14.014686154499964 * U.T + 1.0;
Bc_DT = 11.449906988970557 * U.T;
kp = (((-eig_des_K_control_idx_0 + -b2) + Ac_DT) + 1.0) / Bc_DT;
Ac_DT = ((Bc_DT * kp - Ac_DT) + -b2 * -eig_des_K_control_idx_0) / Bc_DT * DW_l.ec_bar_Delay_DSTATE + kp * ec_bar_d;
In this case the variable "eig_des_control" (a 2x1 vector) is splitted in 2 variables, the code generator called them as "eig_des_K_control_idx_0" and "b2": the last one is called with another variable name I've used in another part of the model.
This is not the only case, I saw many of this changes in my generated code.
I want to know if there are code generation settings or naming rules for these function blocks to avoid this mixing of names, and possibly select what name give to the variables in the final code.
Thanks in advance
Mark McBroom on 22 Mar 2020
Try adjusting the "Loop unrolling threshold" to 0 or 1. What you are encountering is an optimization. It is usually more efficient to operate on each element of a small vector rather than using a for loop.https://www.mathworks.com/help/releases/R2020a/rtw/ref/optimization-pane-general.html