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

조회 수: 2 (최근 30일)
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일
편집: Harsh 2025년 1월 31일
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 CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by