Matlab Coder, set the size of a formal parameters array as indicated by another formal parameter

6 ビュー (過去 30 日間)
V Latorre
V Latorre 2020 年 9 月 3 日
編集済み: Harsh 2025 年 1 月 31 日 11:20
I want to create a c version of the function 'foo' from Matlab with the Matlab coder. The function is called in matlab by the following main script:
N=7;
temp=ones(N,1);
res=foo(N,temp)
By using the Matlab coder, it sets the dimension of temp to 7 in the c code.
How do I tell the Matlab coder that the variable 'temp' in c must have dimension N?

回答 (1 件)

Harsh
Harsh 2025 年 1 月 31 日 11:20
編集済み: Harsh 2025 年 1 月 31 日 11:20
The dimension of “temp” in your code is fixed as “N” is statically defined to be 7. Therefore, when the C code is generated using MATLAB coder it optimizes the code and generates code for a fixed size array. To explicitly define variable-size data, use the function coder.varsize” and you can take N as an input in your main script. Below is an example code snippet to achieve this –
function res = callFoo(N)
%#codegen
temp=ones(N,1);
coder.varsize('temp', [10 1], [1 0]); % only the first dimension is variable with upper bound of 10
res=foo(N,temp);
end
To learn more about code generation for variable sized arrays please refer to the following documentation - https://www.mathworks.com/help/simulink/ug/what-is-variable-size-data.html

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by