Simulink: Variable inferred as variable size matrix when defined from constant blocks
1 回表示 (過去 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
2021 年 10 月 13 日
Hi, did you solve the problem? I need call the mentioned function in simulink too.
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);
![16dec1.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/262025/image.png)
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!