Simulink: Variable inferred as variable size matrix when defined from constant blocks

조회 수: 4 (최근 30일)
Dear all,
I am in need of calling a C-function from within a simulink model. I am using R2017b and do not have the .m, but only the .c and .mexw64 files, hence I've been using a Matlab-function block which looks like this:
function [zopt_fmpc,vopt_fmpc,ctime_mpc,Zw,Vw] = call_fmpc_step(sysfmpc,params,Zw,Vw,ze)
zopt_fmpc=zeros(2,40);
vopt_fmpc=zeros(2,40);
ctime_mpc=1;
coder.extrinsic('fmpc_step')
[zopt_fmpc,vopt_fmpc,ctime_mpc] = fmpc_step(sysfmpc,params,Zw,Vw,ze);
end
This works perfectly fine, expect that I would like to define the size of zopt_fmpc and vopt_fmpc from workspace constants.
I tried
function [zopt_fmpc,vopt_fmpc,ctime_mpc,Zw,Vw] = call_fmpc_step(sysfmpc,params,Zw,Vw,ze,N,n,m)
zopt_fmpc=zeros(n,N);
vopt_fmpc=zeros(m,N);
ctime_mpc=1;
coder.extrinsic('fmpc_step')
[zopt_fmpc,vopt_fmpc,ctime_mpc] = fmpc_step(sysfmpc,params,Zw,Vw,ze);
end
But this results in an error because the compiler sees zopt_fmpc and vopt_fmpc as variable size variables. Now, the values of n, m and N are indeed user defined but remain constant throughout the simulink model simulation.
Is there any way I can circumvent this issue that does not mean having to update the size of the variables manually everytime I change n, m or N?
Thank you!
  댓글 수: 2
QG
QG 2021년 10월 13일
Hi, did you solve the problem? I need call the mentioned function in simulink too.
Bernardo Hernandez
Bernardo Hernandez 2021년 10월 13일
Hello, I was always able to run the fmpc function as I said on my original post, but I never got to read the answer I received from GM so I left it as is. I changed the values of n, m and N manually when I needed.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guru Mohanty
Guru Mohanty 2020년 1월 16일
Hi, you can do that by changing the size of that input as Variable Size. Go to the Function
Editor > Edit Data > Mark as Variable Size
Here is a sample example. For the matrix ‘out’ the dimensions are user-defined.
function ma_x= Input_test(m,n)
out=randi(100,m,n);
ma_x1=max(out);
ma_x=max(ma_x1);
disp(ma_x);
16dec1.PNG

카테고리

Help CenterFile Exchange에서 Schedule Model Components에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by