how to assign values in for loop to a matrix in matlab function block?

8 次查看(过去 30 天)
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?

采纳的回答

Stefan Raab
Stefan Raab 2016-4-23
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by