Matlab coder variable array declaration

조회 수: 11 (최근 30일)
Srikanth
Srikanth 2012년 9월 27일
Hello. I want to declare and use a variable size array in matlab coder. Example as below:
total_size_temp = num_time_steps * num_users; total_size = ones(total_size_temp,1);
% allocate output matrix space x = ones(total_size,3);
How to declare the variable total_size as a variable array in matlab coder ? I tried declaring the variable as below :
coder.varsize('total_size',[total_size_temp,1],[1,0]);
But i get error : This expression must be constant because its value determines the size or class of some expression. Help me in declaring a variable size array.
Thanks Srikanth

답변 (1개)

Shashank
Shashank 2012년 9월 27일
Hi Srikanth,
In this specific example, the issue is that the upper bound [total_size_temp,1] is not known when you compile. Hence, MATLAB Coder is unable to allocate the right amount of memory.
The right way to go about it would be specify any kind of upper bound which total_size might have, or in the worst case, specify an upper bound of [inf,1].
For example, the following code:
function total_size = mlcodervarsize(num_time_steps, num_users) %#codegen
total_size_temp = num_time_steps * num_users;
coder.varsize('total_size',[inf,1],[1,0]);
total_size = ones(total_size_temp,1);
  댓글 수: 2
Srikanth
Srikanth 2012년 9월 28일
Hello. I tried the above solution. It gives an error :
Declaration of upper bound size as Inf is not supported.
I am using Matlab R2011a version. How to solve this error ?
Thanks
Srikanth
Mike Hosea
Mike Hosea 2012년 9월 28일
Shashank's answer only mentioned inf for the worst case. I guess R2011a did not support dynamic (malloc'd) arrays, so you will have to pick a finite upper bound that works for your application. It is also good practice to add an assert to enforce your choice. So if you believe total_size_tmp does not exceed M = 10000, then you might write
M = 10000;
total_size_temp = num_time_steps * num_users;
assert(total_size_temp <= M,'Array too large.');
coder.varsize('x',[M,1],[1,0]);
x = ones(total_size_temp,1);

댓글을 달려면 로그인하십시오.

카테고리

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