Simulink: Variable inferred as variable size matrix when defined from constant blocks
2 次查看(过去 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 个评论
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
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!