MATLAB: Am I receiving a “Size argument must be constant” error when compiling the Embedded MATLAB code in Simulink 7.5 (R2010a)

simulink

When I compile the following Embedded MATLAB code:
function y = StructInitialization
A.B=5;
C= repmat([1 2 3], 1, A.B);
y=0;
end
I receive the following error:
Size argument must be constant.
Function 'Embedded MATLAB Function' (#29.54.77), line 3, column 8:
"repmat([1 2 3], 1, A.B)"
Launch diagnostic report.

Best Answer

This error occurs because constant-folding does not work on structures which are initialized on a field-by-field basis.
To avoid this error, rewrite the expression:
A.B=5;
as
A=struct('B',5);