I am getting errors on my plot of live data collected from a rover.
Show older comments
I am working on code that tracks the path of a rover and plots that path along with any money it encountered. (the code for movment and image recognition of money is correct but im getting errors such as index exceeds aray bounds for the plotting portion. The code in question is all the code under where origin is defined.
n = 0;
while n < 2
d = 0;
while d == 0
speed = 60; %Speed of Robot
obstacle_distance = 20; %Still working on calibrations for this distance
d = gorover(speed, obstacle_distance, rvr)
end
tStart = tic;
while toc(tStart)<10
detectsmoney = detectmoney(rvr);
if detectsmoney == 1
n = n+1;
money = rvr.getPosition;
end
end
rvr.turnAngle(100)
pause(2)
rvr.setDriveSpeed(speed)
clear tStart
d = 0
end
rvr.stop
fprintf('I detected %d dollars',n);
if n > 0
foundmoney = true;
else
foundmoney = false;
end
foundTotal = ShownTotal(rvr,n);
origin = [0, 0];% starting position
position = origin;
path = position; % path with starting position
if path ~= [0, 0]
movement = rvr.getPosition ;
end
scatter(position(1), position(2),'b', 'filled');
hold on;
for i = 1:size(movement, 1)
% Update position and path
position = position + movement(i,:);
path = [path; position];
% Plot path
plot(path(:,1), path(:,2));
% Plot money locations
found_money = ismember(moneyamt, position, 'rows');
if any(found_money)
scatter(position(1), position(2), 'g', 'filled');
end
end
moneyamt = scatter(money(2,1), money(2,2), 'g', 'filled');
title('Position')
legend([origin,moneyamt],{'Origin', 'Money'});
pause;

Answers (1)
You are trying to index to an element in your variable that does not exist.
Position 1 is your rows index, The error message is telling you that there is only one row, and I see you are trying to index to row 2.
A = 1:3
% Works
A(1,2)
% your error
A(2,2)
5 Comments
Grant
on 13 Apr 2023
Cris LaPierre
on 13 Apr 2023
Edited: Cris LaPierre
on 13 Apr 2023
You should inspect the value of rvr.getPosition so you know what data you are working with (see here).
Based on the error you are getting, it appears to only return one postion, so why not try plotting row 1 of money instead of row 2 and see if that is what you expect.
Grant
on 13 Apr 2023
Grant
on 13 Apr 2023
I also can't connect to the rover, so much of this troubleshooting will have to be done by you. Note the following:
money = [3 5; 2 6;]
A semicolon creates a new row, and a space separates columns. If your data contained more than one point, then you would not have gotten the error you got.
If you don't know how many rows are in your data, but always want to plot the last row, then use 'end' instead of a row number.
money = [3 5; 2 6;];
plot(money(end,1),money(end,2),'o')
Categories
Find more on MATLAB 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!