MATLAB: How to assign values in for loop to a matrix in matlab function block

MATLABmatlab function blockmatrixsimulinksize mismatch error

Hi, I have following code ,
N = 400; phi = []; for kk = 1:5 phi = [phi; exp(j*2*pi*(kk/N)*(0:N-1))]; end
I need to create 5 x 400 matrix for phi. I'm using this code in a matlab function block. but I'm getting a size mismatch error. any ideas?

Best Answer

Hello,
the MATLAB Function block has limitations due to code generation. Either you preallocate the memory for phi and assign it as a comlex variable in the first definition, or you write an external "normal" MATLAB function that you call from inside the MATLAB Function block. The latter will then only work if you define your normal function as extrinsic inside the MF block (doc coder.extrinsic). This will work fine in a simulation, but if you want to generate code from the Simulink model, the coder.extrinsic won't work anymore.
Here is a sample code for the first option:
N = 400;
phi = 1i*ones(5,400);
for kk = 1:5
phi(kk,:) = exp(1j*2*pi*(kk/N)*(0:N-1));
end
This worked for me. I hope this might help you.
Kind regards, Stefan