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