Name array collumns using user input
1 view (last 30 days)
Show older comments
Hi, I'm trying to code for a user to name collumns within arrays as an input.
My code first asks the user to enter floor dimensions of a building followed by room dimensions and as the next step I'm trying to code for the user to be able to specify what the purpose of the room is from one of the following options (perhaps a dialogue box) and then get this data to be used to name a collumn.
The options are:
Residential, Office, Education, Toilet, Storage
Here is my code so far for understanding:
clear;
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
3 Comments
Walter Roberson
on 15 Apr 2022
Ordinary MATLAB arrays cannot have column names. You can have separate variables that you use in conjunction with ordinary arrays, manually doing the conversion yourself.
However, the MATLAB table() and timetable() datatypes can have column names. To change the name a column, assign to the appropriate table Properties.VariableNames item; for example,
MyBuildingTable.Properties.VariableNames{3} = 'Storage';
Answers (1)
Pavan Sahith
on 5 Oct 2023
Hi Tobias,
I understand that you want to take input of column names from the user and populate your array with those column names and their corresponding data.
But , in MATLAB, arrays are designed to store homogeneous data, meaning all elements in an array must be of the same data type.
In this case, you shall need to store heterogeneous data, where elements can have different data types. In MATLAB, you can achieve the same using cell arrays and structure arrays.
Pleaserefer the below attached MathWorks Documentation links for more information on ‘Cell array’ and ‘Staructure array’ in MATLAB
Further, please know that the MATLAB `table ()` and `timetable()` datatypes support column names. Hence, to change the name of a column, you can simply assign a new value to the corresponding `Properties.VariableNames` item of the table
For more details, please refer the following MathWorks Documentation link on ‘Table’:
Besides, to obtain the purpose of the room from the user using a dialogue box, you can utilize the “listdlg” function. I have attched an example code below for your understanding:
% Define the question and answer options
question = 'Select the purpose of the room:';
answerOptions = {'Residential', 'Office','Education','Toilet', 'Storage'};
% Create the dialog box with a dropdown menu
[selection, ~] = listdlg('PromptString', question, 'SelectionMode', 'single', 'ListString', answerOptions,'ListSize',[300,150]);
% Perform actions based on the selected answer
if ~isempty(selection)
selectedPurpose = answerOptions{selection};
disp(['Your selected purpose is: ' selectedPurpose]);
else
disp('No purpose selected.');
end
For more details on using the “listdlg” function, please follow the below provided MathWorks Documentation link
0 Comments
See Also
Categories
Find more on Point Cloud Processing 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!