how to assign values in for loop to a matrix in matlab function block?
7 views (last 30 days)
Show older comments
Chamith Jayasinghe
on 22 Apr 2016
Commented: Chamith Jayasinghe
on 25 Apr 2016
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?
0 Comments
Accepted Answer
Stefan Raab
on 23 Apr 2016
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
More Answers (0)
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!