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

2 views (last 30 days)
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?

Answers (1)

Harsh
Harsh on 31 Jan 2025
Edited: Harsh on 31 Jan 2025
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

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!