Creating a new graph for each row in a matrix

8 visualizaciones (últimos 30 días)
Tobias Weymouth
Tobias Weymouth el 1 de Mayo de 2022
Comentada: Voss el 2 de Mayo de 2022
Hi, I've got a MATLAB code where I get the user to input dimensions for a building and specify one coordinate then it is plotting on the graph. I'm using the rectangle function and at the moment it plots all of the dimensions on one graph so the rooms on different floors overlap.
Does anybody know how and can offer me some help in adjusting my code to get each row of the matrix (this refers to each floor) to plot onto a different graph/set of axis?
Here is the part of code I'd imagine needs something adding to:
for j=1:1:(numberRooms(i))
rectangle('Position',[coordinatesX(i,j) coordinatesY(i,j) roomWidth(i,j) roomLength(i,j)])
hold on
end
And here is my full code:
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
for j=1:1:(numberRooms(i))
rectangle('Position',[coordinatesX(i,j) coordinatesY(i,j) roomWidth(i,j) roomLength(i,j)])
hold on
end
end
Thanks for any help :))
  2 comentarios
Jonas
Jonas el 1 de Mayo de 2022
you could create a tileylayout('flow') before 'for i=1:1:numberFloors' and then call nexttile() at the beginning of each iteration of i
Tobias Weymouth
Tobias Weymouth el 2 de Mayo de 2022
@Jonas thanks for the help :)

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 1 de Mayo de 2022
One fairly easy way to separate the floorplans is to plot each one into its own axes (using tiledLayout or subplot, for instance). You could make a new axes at the beginning of each iteration of the for i=1:1:numberFloors loop:
% ... (your existing code continues above)
for i=1:1:numberFloors
subplot(numberFloors,1,i);
title(sprintf('Floor %d',i));
% ... (your existing code continues below)
end
Another alternative would be to include a 3rd dimension in your plots, where the z-coordinate of the plotted objects corresponds to the height of their floor above the ground. I'm not sure, but looking at the documentation for rectangle, I don't see any way to vary their location in the z-direction, so maybe you'd have to use something else, like patches.
  2 comentarios
Tobias Weymouth
Tobias Weymouth el 2 de Mayo de 2022
@_ that's working now thanks!
Voss
Voss el 2 de Mayo de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots 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