How do I avoid hardcoding output size when using simulink coder.extrinsic?

8 ビュー (過去 30 日間)
Josiah Wai
Josiah Wai 2023 年 5 月 1 日
コメント済み: Josiah Wai 2023 年 5 月 6 日
I have a matlab function 'control_fun' that I'm using in a simulink model. It does not compile with codegen so I'm using coder.extrinsic to run the model. The problem is that I have to hardcode the size of the output of controlfun when I would like this to be parameter that can change (not during a simulation, but between simulations). I use a simulink matlab-function to call the control_fun.
This works:
function u = call_control_fun(data, parameter) % parameter = 5
coder.extrinsic('control_fun')
u = zeros(5,1);
u = control_fun(data)
But this doesn't:
function u = call_control_fun(data, parameter) % parameter = 5
coder.extrinsic('control_fun')
u = zeros(parameter, 1);
u = control_fun(data)
This results in an error:
Block ''call_controlfun'' does not fully set the dimensions of output 'u'.
I have tried playing with coder.varsize but haven't gotten anything to work.
  1 件のコメント
Josiah Wai
Josiah Wai 2023 年 5 月 1 日
update: an okay-ish workaround is to use a large fixed output size with zero-padding, then elsewhere in the model use variables to index into u and remove the zero-padding.

サインインしてコメントする。

採用された回答

Fangjun Jiang
Fangjun Jiang 2023 年 5 月 5 日
編集済み: Fangjun Jiang 2023 年 5 月 5 日
I suspect that the "parameter" in your code is not really a parameter-by-definition by the MATLAB Function block. It appears as an Inport to the MATLAB Function block, does it?
In MATLAB Function block editor, click "Edit Data", change the "Scope" or "Type" of "parameter" from "Input" to "Parameter". Then, "parameter" won't appear as an Inport to the MATLAB Function block, it becomes a parameter of the block, just like any other parameters of any other Simulink blocks. You set its value in base workspace or model workspace. Then the error you had should disappear.
  1 件のコメント
Josiah Wai
Josiah Wai 2023 年 5 月 6 日
Thank you, it was set as a parameter but the workaround is okay for now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEvent Functions についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by