Asked by Orpheus
on 17 Sep 2019

In the given code, waypoints is a 3x3 matrix. The error comes in line 8.

persistent waypoints0 traj_time d0

if nargin > 2

d = waypoints(:,2:end) - waypoints(:,1:end-1);

d0 = 2 * sqrt(d(1,:).^2 + d(2,:).^2 + d(3,:).^2);

traj_time = [0, cumsum(d0)];

waypoints0 = waypoints;

else

if t >= traj_time(end)

t = traj_time(end);

end

t_index = find(traj_time > t,1) - 1;

if (t_index == 0)

t_index = 1;

end

if(t == 0)

desired_state.pos = waypoints0(:,1);

else

scale = (t-traj_time(t_index)) / d0(t_index);

index = [(t_index-1)*8+1:t_index*8];

end

end

Answer by Guillaume
on 17 Sep 2019

Edited by Guillaume
on 17 Sep 2019

Accepted Answer

You should read the comments at the top of the function and understand them.

In particular, the function must be initialised first by calling it with

trajectory_generator([], [], waypoints);

where waypoints is a 3xN matrix.

Only then can you use it with

desired_state = traj_generator(t, state);

The error you see occurs when traj_time is empty (end is indeed an invalid index in that case). And traj_time can only be empty if the function has not been initialised.

Granted, the function should check that it's been initialised before using the persistent variables. You should add than check.

...and take out the nested functions which should be local functions

...and indent the code properly to make it easier to read and debug

...and fix all the missing semicolons and any other mlint warning

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## KALYAN ACHARJYA (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480816-getting-an-array-indices-must-be-positive-integers-or-logical-values-error#comment_746928

## Aquatris (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480816-getting-an-array-indices-must-be-positive-integers-or-logical-values-error#comment_746932

## Guillaume (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480816-getting-an-array-indices-must-be-positive-integers-or-logical-values-error#comment_746939

## Orpheus (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/480816-getting-an-array-indices-must-be-positive-integers-or-logical-values-error#comment_746951

Sign in to comment.