Setting a fixed sized matrix size in Simulink at run time

11 ビュー (過去 30 日間)
dvd7e
dvd7e 2021 年 1 月 6 日
コメント済み: 창현 김 2022 年 9 月 20 日
In Simulink I would like to create a matrix that is of fixed size during a run (e.g. as opposed to a variabel sized matrix), but to have those dimensions be tuneable from run to run based on a parameter. Is this possible?
For example, I would like to be able to do something along the lines of the following, which would be as embedded matlab within Simulink
function A = myEmbeddedMatlabFcn(n)
% Note: n is a *Parameter" defined in the workspace, and NOT an input!
A = randn(n,n);
where n might be 5 for one simulation run, and 3 for another simulation run, but in both cases it is constant during the respective simulation runs, it is only varies across runs.
In theory if I define "n" in the workspace before Simulink compiles, I don't see why it shouldn't be able to handle this. But it can't....it always chokes and says that "Data 'n' is inferred as a variable size matrix..." even though it should be constant.
(Note: For my specific application, I am aware of, but would like to avoid, a few possible workarounds: a) variable sized matrices and b) pre-allocated buffers that are larger than necessary and then only use/work on a subset of data.)
Thank you

採用された回答

Timo Dietz
Timo Dietz 2021 年 1 月 6 日
First, add an input 'n' of type „Parameter“ to the Matlab function block using the Simulink ModelExplorer and un-check the “Tunable” option.
Mask the parent block of the function and add a dialog parameter which is stored to internal variable 'n'.
Now, the variable name you state in the input box of the mask will be taken from workspace and propagated to n for your function.
  3 件のコメント
dvd7e
dvd7e 2021 年 1 月 11 日
Yup, that did it, thank you!
창현 김
창현 김 2022 年 9 月 20 日
Wow! Thank you so much.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by