1
votes

I have some code like this in my matlab function:

for i = init:end
    a= [a, char(zeros(size(a))]
    a= func(a)
    a = a(a~=0)
end

So, in each step of the loop, you first double the size of a, apply some random function to it, and then remove everything that is a zero.

Now, I want to run coder on this to eventually make it into c code. Of course, it gives me allocation errors.

Allowing dynamically allocated arrays doesn't help. I can't simply use different names for my variables either because it is in a for loop.

1
what is the type of elements of a? did you display the result to see what you are getting from one iteration to another? - innoSPG
As I said, this is an example code. a could be anything. The code works in matlab, but coder is having trouble making this into a c code. - user e to the power of 2pi
you may consider running the example code and make sure it is working too, so that you get the best suggestion. - innoSPG

1 Answers

1
votes

This may come a year late however something like:

function a = foobar(init,ub)
coder.varsize('a',[1,Inf]);
a = 1;
for i = init:ub
    a = [a, ones(size(a))];
    a = sin(a);
    a = a(a~=0);
end

>> codegen foobar -args {1,1} -report

works for me in R2014a.

When you are trying to grow an array, it may be necessary to use coder.varsize in order to tell MATLAB Coder that the array is going to change size. In many cases MATLAB Coder can figure this out without using coder.varsize so it is best to try without it first and then add it in if errors are encountered.