Index in position 1 exceeds array bounds. Erroy when trying to plot rectangle.

2 views (last 30 days)
Hi, I'm trying to plot a rectangle given a corner coordinate and then 2 dimensions. I can get it working for one set of coordinates and dimensions in a array but no more than that when I input into my code that I have more than one room.
Here is the error code I get: Index in position 1 exceeds array bounds. Index must not exceed 1.
Does somebody know how to fix this. In my code the user inputs a given number of floors, rooms and dimensions (in addition to the style of the room but this doesn't affect this section). See my code below:
Thanks in advance for any help :)
clear;
numberFloors=input('Please input the number of floors required: ');
if numberFloors<=0
disp('Number of floors must be at least 1');
return
else
disp('Thanks');
end
floorLength=zeros(1,numberFloors);
floorWidth=zeros(1,numberFloors);
floorHeight=zeros(1,numberFloors);
for i=1:1:numberFloors
floorLength(i)=input(['Please enter floor ',num2str(i),' length: ']);
floorWidth(i)=input(['Please enter floor ',num2str(i),' width: ']);
floorHeight(i)=input(['Please enter floor ',num2str(i),' height: ']);
numberRooms(i)=input(['Please enter the number of rooms on floor ',num2str(i),': ']);
if floorLength(i)<=0 || floorWidth(i)<=0 || floorHeight(i)<=0 || numberRooms(i)<=0
disp('Floor dimensions and number of rooms must be greater than 0');
return
end
for j=1:1:(numberRooms(i))
roomLength(i,j)=input(['Please enter room ',num2str(j),' length: ']);
roomWidth(i,j)=input(['Please enter room ',num2str(j),' width: ']);
roomHeight(i,j)=input(['Please enter room ',num2str(j),' height: ']);
if roomLength(i,j)<=0 || roomWidth(i,j)<=0 || roomHeight(i,j)<=0
disp('Room dimensions must be greater than 0');
return
end
list={'Residential','Office','Education','Toilet','Storage'};
[indx,tf]=listdlg('ListString',list,'PromptString','Style of Room');
roomType(i,j)=indx;
answer = inputdlg({'X Coordinate','Y Coordinate'},'Room Coordinates',[1 50]);
coordinatesX(i,j) = str2num(answer{1});
coordinatesY(i,j) = str2num(answer{2});
end
n = numberRooms(1,1);
for i = 1:1:n
rectangle('Position',[coordinatesX(numberRooms,i) coordinatesY(numberRooms,i) roomWidth(numberRooms,i) roomLength(numberRooms,i)])
end
end

Accepted Answer

DGM
DGM on 29 Apr 2022
I don't know why you decided to suddenly abandon your indexing scheme. Replace all this:
n = numberRooms(1,1);
for i = 1:1:n
rectangle('Position',[coordinatesX(numberRooms,i) coordinatesY(numberRooms,i) roomWidth(numberRooms,i) roomLength(numberRooms,i)])
end
With this.
for j=1:1:(numberRooms(i))
rectangle('Position',[coordinatesX(i,j) coordinatesY(i,j) roomWidth(i,j) roomLength(i,j)])
hold on
end
  3 Comments
DGM
DGM on 2 May 2022
If you call figure() in the outer loop prior to the loop that does the plotting, then it should create a new figure for each floor.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!