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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

DGM
DGM el 29 de Abr. de 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 comentarios
DGM
DGM el 2 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by