Matlab coder variable array declaration
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 FPGA, ASIC, and SoC Development 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!