Allow arbitrarily sized inputs for codegen without infinite bounds on array size

1 view (last 30 days)
Hello, I am playing with the code generator (R2014b) and I am trying to allow an input to be an arbitrarily sized array with a fixed number of dimensions. This code (designed to allow me to test the speed of a random loop) seems to work:
function outVal = myFuncTest(inVal) %#codegen
assert(isa(inVal,'double'));
assert(all(size(inVal) <= [inf inf]));
outVal = zeros(100,1);
for ii = 1:100
for jj = 1:numel(inVal)
outVal(ii) = 3.*myFunc(inVal(jj)).*(ii./100);
end
end
end
function outVal = myFunc(x)
outVal = (x.^2)/2;
end
end
The key to make it work seems to be this line:
assert(all(size(inVal) <= [inf inf]));
but it doesn't seem right to me to have to use infs as an upper limit to array size. I have tried to use the coder.varsize option by putting:
coder.varsize('inVal',[]);
as the first line of the function instead, but when I do, I get an error saying that:
??? Size of inVal cannot be modified as the variable inVal is already locked.
Dimension 1 cannot be made dynamic: it has previously been set to have fixed size 1.
If I leave out both statements, the code compiles but then gives an error at runtime, since the coder seems to explicitly need variable size to be given:
myFuncTest_mex(ones(100))
MATLAB expression 'inVal' is not of the correct size: expected [1x1] found [100x100].
I would be grateful if someone could point out to me the correct way of achieving this, or let me know if there is any practical problem with using the infinite bounds on variable size.

Accepted Answer

Ryan Livingston
Ryan Livingston on 19 Mar 2018
Using Inf is the standard way to specify unbounded array sizes for Coder. You can use that with assert or with varsize: coder.varsize('inVal',[Inf,Inf]);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!