MATLAB: Matlabfunction generates redundant copies of variables

functionmatlabfunctionoptimizerepeatslowsymbolicSymbolic Math Toolboxvariables

Best Answer

The reason why it is slow and creating all those variables is because 'Optimize' is set to true by default. This analysis can be very costly so setting 'Optimize' to false will resolve this issue.

>> matlabFunction(symbfunc, 'File', fileName, 'Optimize', false);

Please note that while having Optimize set to true does create redundant variables, the duplication should not affect the performance of the generated C++ code.