Create MATLAB Function block with output size set at compile time

3 ビュー (過去 30 日間)
NJ2Cali
NJ2Cali 2023 年 6 月 20 日
コメント済み: NJ2Cali 2023 年 6 月 21 日
I've created a Matlab function block that takes in a parameter n from a parent mask and outputs an array of size [n,1]. I intend on using it for code generation, so the output must be fixed size.
In theory, since the mask parameters are set in the initialization script at compile time it should be possible to create a fixed size array using the parameter n.
However, when I set the output to a fixed size
and put the following code in the MATLAB function:
function y = fcn(u, n)
% preallocate array size
y = zeros(n,1);
y(:) = ones(1,n)*u
end
I receive a
"Data 'y' is inferred as a variable size matrix, while its properties in the Model Explorer specify its size as inherited or fixed. Please check the 'Variable Size' check box and specify the upper bounds in the size field."
error.
It seems that MATLAB does not recognize "n" as a parameter that won't change during the simulation. Is there any way around this?

採用された回答

Govind KM
Govind KM 2023 年 6 月 21 日
Hi NJ2Cali,
It is possible that the variable n was defined as a Tunable parameter within the MATLAB Function block when it was created. This leads to Simulink interpreting the y array as variable sized because n could theoretically change during the simulation, even though it's value might not change in your case. If this is the case, To resolve this, you can set n to be non-tunable using the 'Edit Data' option from within the MATLAB Function Block.
You can refer to these answers for a little more info on this :

その他の回答 (1 件)

Aman
Aman 2023 年 6 月 21 日
Hi,
You can try adding the line,
u = u(1);
before
y(:) = ones(1,n)*u

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by