Creating a new graph for each row in a matrix

조회 수: 1 (최근 30일)
Tobias Weymouth
Tobias Weymouth 2022년 5월 1일
댓글: Voss 2022년 5월 2일
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
Jonas
Jonas 2022년 5월 1일
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 2022년 5월 2일
@Jonas thanks for the help :)

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 5월 1일
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
Tobias Weymouth
Tobias Weymouth 2022년 5월 2일
@_ that's working now thanks!
Voss
Voss 2022년 5월 2일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by