Matlab coder variable array declaration

5 vues (au cours des 30 derniers jours)
Srikanth
Srikanth le 27 Sep 2012
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

Réponses (1)

Shashank
Shashank le 27 Sep 2012
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 commentaires
Srikanth
Srikanth le 28 Sep 2012
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 le 28 Sep 2012
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);

Connectez-vous pour commenter.

Catégories

En savoir plus sur FPGA, ASIC, and SoC Development dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by