Creating a new graph for each row in a matrix
1 view (last 30 days)
Show older comments
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 Comments
Jonas
on 1 May 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
Accepted Answer
Voss
on 1 May 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.
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots 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!