MATLAB Answers

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

26 views (last 30 days)
Bernardo Hernandez
Bernardo Hernandez on 16 Sep 2019
Commented: Bernardo Hernandez on 13 Oct 2021 at 10:38
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 Comments
Bernardo Hernandez
Bernardo Hernandez on 13 Oct 2021 at 10:38
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.

Sign in to comment.

Answers (1)

Guru Mohanty
Guru Mohanty on 16 Jan 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

Community Treasure Hunt

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

Start Hunting!

Translated by