Clear Filters
Clear Filters

Index in position 1 is invalid. Array indices must be positive integers or logical values.

3 views (last 30 days)
I am getting an error in line: while m(xo,yo)==0 %if the location not occupied
the error is '' Index in position 1 is invalid. Array indices must be positive integers or logical values''
and my code is:
xo=-13; yo=-13; th=pi/2;
x=-13; y=-13;
rr=1; %Robot radius
r=rr; %ray vector
u=Control(); % control function
Unrecognized function or variable 'Control'.
n=4; %Number of sensors
dbstop if error
for t=1:size(u,1)
if u(2,t)~=0
w=100*eps; % 100*eps because eps alone is too small, so causes division by zero problem
m=Map(); %map function
x=x-v/w*sin(th)+v/w*sin(th+w*dT);%state update
y=y+v/w*cos(th)-v/w*cos(th+w*dT);%state update
th=th+w*dT;%state update
for k=0:n-1
rr=1; r=rr;
xo=xo+r*cos(th); %corridnates(x,y) for the robot location
yo=yo+r*sin(th); %r is length(r)
while m(xo,yo)==0 %if the location not occupied %%% my error in this line
if m(xo,yo)==1 %if the location occupied
mesh(xo,yo,m); view(0, 90); axis image; hold on;

Answers (2)

Paul Hoffrichter
Paul Hoffrichter on 19 Apr 2021
Edited: Paul Hoffrichter on 19 Apr 2021
Would be very useful if you are able to post a minimal program exhibiting the error that we could run. In the debugger, set the Run button to "Pause on Error". When the program breaks at the error, check that xo and yo are scalars, and check that they are both greater than 0.
GHADAH AL-OBAIDI on 21 Apr 2021
I have to test each point in the map if it's occupied or not, then if it's not occupied the robot should keep moving but if it's occupied then I should label the empty map with blue dot or any mark.
Paul Hoffrichter
Paul Hoffrichter on 21 Apr 2021
Edited: Paul Hoffrichter on 21 Apr 2021
From your stated requirements, I do not understand your left and right output figures. I think you may have a requirements and design issue rather than a Matlab question.
For a general programming point of view (whether Matlab or not), you should get the program to run with a number of well defined functions each having a clearly stated purpose. There should be more comments so that all can understand your design. One function might be CreateInitialMaze(). Your main script should be fairly small and devoid of math computations. Design the main script with as many high-level functions as you need to do your job.
When I said, "Please put all your code in a code block.", I did not mean for you to eliminate your functions.
Unless you have a Matlab question, I recommend closing this question, and starting fresh with the general programming principles I suggested.

Sign in to comment.

Walter Roberson
Walter Roberson on 22 Apr 2021
You initialize a playing field of free space and obstacles.
You start at some particular location.
At any one point, you have a goal that you use to figure out which direction you would ideally like to go. You test to see if you can go in that direction. If so, you go there and loop back to the next step. If you cannot go in the most desired direction, you figure out the next best direction to go, and see if you can go there; if you cannot, then the next best after that, and so on. Unless this was the very first step and you happened to land in the one open spot surrounded by barriers, there is always somewhere you can go.
You will, however, find that in practice you need to be a bit more clever than the above.
1 R 2
5 6 7
Let R be the robot, and G be the goal. The "ideal" direction would be for the robot to move downward. But of course if it does, then it cannot go any further towards G, and needs to move back to [1], or the location it came from [R], or to [2] . If it moves back to [R] then it gets back to the place where the "ideal" move is straight down towards G... getting stuck again. If it moved to [1] or to [2], then in the next step, the "ideal" move would be to move diagonally down, getting stuck in the same rut again.
How to deal with this, how to avoid cycling through positions due to lack of long-term planning, or due to limited "visibility" not permitting you to know what the "best" step is, is key to robotics. You can research algorithms for this, or you can try to come up with one yourself.

Community Treasure Hunt

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

Start Hunting!