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

조회 수: 11 (최근 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