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

4 visualizaciones (últimos 30 días)
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 comentarios
QG
QG el 13 de Oct. de 2021
Hi, did you solve the problem? I need call the mentioned function in simulink too.
Bernardo Hernandez
Bernardo Hernandez el 13 de Oct. de 2021
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.

Iniciar sesión para comentar.

Respuestas (1)

Guru Mohanty
Guru Mohanty el 16 de En. de 2020
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

Categorías

Más información sobre Schedule Model Components en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by