Using indexed data to colour graph

15 views (last 30 days)
Tobias Weymouth
Tobias Weymouth on 2 May 2022
Commented: dpb on 3 May 2022
Hi, I've got a piece of code which asks the user to select the use of a room for a floorplan plotting code. The code currently produces a dialoge list allowing the user to select the purpose of the particular room they're entering dimensions for this is then put into an index array.
In the list:
Residential (list) = 1 (array)
Office (list) = 2 (array)
Education (list) = 3 (array)
This continues for the 5 options so MATLAB stored the data the user has entered in an array with a collumn for each room and a row for each floor. I have MATLAB plotting these rooms on a graph for each floor now I'm trying to get MATLAB to use this inputted data to color code the graph to indicate on the floorplan the type of room. For example, if the user says room 2 is a toilet, the graph will have that room coloured in blue. I will also have to put a key of what the 5 colours mean on the graph if that is possible?
I think it will need to go into the code somewhere here:
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
But here is my full code currently:
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
subplot(numberFloors,1,i);
title(sprintf('Floor %d',i));
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
I hope this makes sense to someone.

Accepted Answer

dpb
dpb on 2 May 2022
Edited: dpb on 2 May 2022
See the documentation for the function rectangle() that you're using to do the drawing.
The LH side of the page has links to the following sections, as well as examples show specific cases.
NB: the Name-Value Pair of 'FaceColor'
rectangle
Input Arguments
pos
cur
ax
Name-Value Pair Arguments
FaceColor
EdgeColor
...
Just create a lookup table of the colors you want versus index to use.
As for a label/legend, rectangle doesn't support the 'DisplayName' property that the legend function uses so it's out.
patch ,however, does; why not use it instead, perhaps the simplest thing to do from where you are is to write a small helper routine to create the required array of coordinates it needs from the four point origin/size vector rectangle() uses and call it instead of rectangle directly. This would leave minimal changes to the top-level code as written presently.
ADDENDUM:
The following seems to work standalone; should be adaptable to your case if not direct plug-in--
% create a drop-in to use patch() in place of rectangle() with same inputs
% and a color triplet defined
rect2patch=@(v,c) patch('Vertices',[[v(1),v(2)]; [v(1)+v(3),v(2)]; [v(1)+v(3),v(2)+v(4)]; [v(1),v(2)+v(4)]], ...
'Faces',[1:4], ...
'FaceColor',c);
% Get a default color map for testing...
hAx=gca; % just create a default axes object handle
C=hAx.ColorOrder; % the default axes line colormap array
R=[0 0 2 4]; % a set of coordinates for rectangle
for i=1:2
hP(i)=rect2patch(R,C(i,:));
R=R+[4 1 5 2]; % an arbitrary size, location change to right
end
axis([0 11 0 11]), axis equal, box on
hLg=legend(hP,'Room 1', 'Room 2','Location','eastoutside');
created the following--
Obviously, the legend labels can also be text lookup to match whatever is desired or set the patch object 'DisplayName' and/or 'Annotation' properties. These could be made automagic by including them in the above anonymous function by passing the title.
  2 Comments
Tobias Weymouth
Tobias Weymouth on 3 May 2022
@dpb thanks so much for your help it's been really useful!
dpb
dpb on 3 May 2022
Is kinda' cute, isn't it? :)
That could just use the anonymous function idea didn't come to me until quite a bit later...when wrote the first response I figured would have to have a standalone/internal function to do the translation but then it dawned on me that all had to do was compute the coordinates from the location/size, the rest was fixed.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!