MATLAB Coder indexing out of bounds

3 views (last 30 days)
sita on 9 Jul 2013
function f = objctiv(x)
f(1)=x(1).^2-2*x(2).^2 f(2)=x(10).^2
here x has x1..x10 values. For above function if i try to acsess x2,..x10 it gives error.
Attempted to access element 2. The valid range is 1-1.
how can i grow matlab variables through indexing,Which can be supported by MATLAB Coder. Please suggest me how it could be done.
I found following link for similar problem.
how can i do sugggested thing for my problem.
Thanks, Sita

Answers (1)

Mike Hosea
Mike Hosea on 9 Jul 2013
If x is a scalar passed to this function, then attempting to reference x(2) will error in MATLAB and in MATLAB Coder in exactly the same way for exactly the same reason. This has nothing to do with growing an array by indexing. I suspect you have tried to compile this without specifying the type of x using -args or through the coder GUI. Once you correct that issue, you need to preallocate f by
f = zeros(1,2,class(x));
Now that is because growing arrays by indexing is unsupported. Note that preallocation is preferred in MATLAB.


Find more on MATLAB Coder in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by