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

3 次查看(过去 30 天)
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

采纳的回答

DGM
DGM 2022-4-29
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 个评论
DGM
DGM 2022-5-2
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by