User input various labbelled array data

1 view (last 30 days)
Tobias Weymouth
Tobias Weymouth on 19 Apr 2022
Answered: Shivam on 27 Sep 2023
Hi, I'm very new to MATLAB so I don't know the best way to go about completing the task I've been set and hence why I'm hoping to get some help. Below is an explanation of what I'm trying to achieve perhaps to take a different/better approach.
I'm writing a piece of code to allow a user to plot details about a building in order to graphically represent it and lay it out in a floor plan. So far - at the start of the code I ask the user to enter how many floors in the building. The user then is asked to enter the length, width and height of floor 1 as three seperate questions hitting return each entry. The user then is asked to enter the number of rooms on that floor (i.e. floor 1). The code then asks for each rooms length, width and height in the same way. This continues until all the floors and rooms are complete and generates an array of all the floors lengths, a sererate array for all the floors widths, then another array for all the floor heights. It also does the same for all the room dimensions in the same way so I'm left with many arrays with data which would ideally be better sorted a different way and with a means of describing the purpose of the room as one of the 5 options.
Here is the outputted data for example:
And here is my code so far:
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);
mainlooptotal=1;
while mainlooptotal<=numberFloors
for i=1:1:numberFloors
floorLength(i)=input(['Please enter floor ',num2str(mainlooptotal),' length: ']);
floorWidth(i)=input(['Please enter floor ',num2str(mainlooptotal),' width: ']);
floorHeight(i)=input(['Please enter floor ',num2str(mainlooptotal),' height: ']);
numberRooms(i)=input(['Please enter the number of rooms on floor ',num2str(mainlooptotal),': ']);
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
else
mainlooptotal=mainlooptotal+1;
end
roomLength=zeros(1,(numberRooms(i)));
roomWidth=zeros(1,(numberRooms(i)));
roomlooptotal=1;
while roomlooptotal<=numberRooms
for j=1:1:(numberRooms(i))
roomLength(j)=input(['Please enter room ',num2str(roomlooptotal),' length: ']);
roomWidth(j)=input(['Please enter room ',num2str(roomlooptotal),' width: ']);
roomHeight(j)=input(['Please enter room ',num2str(roomlooptotal),' height: ']);
if roomLength(j)<=0 || roomWidth(j)<=0 || roomHeight(j)<=0
disp('Room dimensions must be greater than 0');
return
else
roomlooptotal=roomlooptotal+1;
end
end
end
end
end
I'm thinking there is probably a better way to input this data perhaps to make an array of each floor or maybe each room rather than how it currently is formatting the inputted data.
I hope this make sense and thanks in advance.
Cheers, Toby

Answers (1)

Shivam
Shivam on 27 Sep 2023
I understand you are trying to know the better way to input and store data of a building.
Using MATLAB struct can be a better way to deal with the requirements of the problem.
You can follow the below workaround to solve the problem.
clear;
numberFloors=input('Please input the number of floors required: ');
if numberFloors<=0
disp('Number of floors must be at least 1.');
return
end
% Initialize an empty array to store the floor data
floors = struct('length', [], 'width', [], 'height', [], 'rooms', []);
% Iterate over each floor
for floorIndex=1:numberFloors
% Prompt the user to enter the dimensions of the floor
floor.length = input(['Enter the length of the floor-',num2str(floorIndex),': ']);
floor.width = input(['Enter the width of the floor-',num2str(floorIndex),': ']);
floor.height = input(['Enter the height of the floor-',num2str(floorIndex),': ']);
% Prompt the user to enter the number of rooms on this floor
numberRooms = input('Enter the number of rooms on this floor: ');
if floor.length <= 0 || floor.width <= 0 || floor.height <= 0 || numberRooms <= 0
disp('Floor dimensions and number of rooms must be greater than 0.');
return;
end
% Initialize an empty array to store the room data
rooms = struct('length', [], 'width', [], 'height', []);
% Iterate over each room on this floor
for roomIndex = 1:numberRooms
% Prompt the user to enter the dimensions and purpose of the room
room.length = input(['Enter the length of the room-',num2str(roomIndex),': ']);
room.width = input(['Enter the width of the room-',num2str(roomIndex),': ']);
room.height = input(['Enter the height of the room-',num2str(roomIndex),': ']);
if room.length <= 0 || room.width <= 0 || room.height <= 0
disp("Room dimension must be greater than 0.");
return;
end
% Add the room data to the rooms array
rooms(roomIndex) = room;
end
% Add the floor data, including the rooms array, to the floors array
floor.rooms = rooms;
floors(floorIndex) = floor;
end
clearvars -EXCEPT floors;
Please refer to the following documentation to learn more about struct in MATLAB: Structure array - MATLAB (mathworks.com)
I hope this helps.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!