Effacer les filtres
Effacer les filtres

Name array collumns using user input

1 vue (au cours des 30 derniers jours)
Tobias Weymouth
Tobias Weymouth le 15 Avr 2022
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:
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');
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');
  3 commentaires
Walter Roberson
Walter Roberson le 15 Avr 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';
Tobias Weymouth
Tobias Weymouth le 18 Avr 2022
Hi @Walter Roberson, thanks for your response. 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 was asking about naming collumns. I thought I'd explain better what it is I'm trying to achieve perhaps to take a different 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:
Hope you're able to help if it isn't too much hassle. Thanks

Connectez-vous pour commenter.

Réponses (1)

Pavan Sahith
Pavan Sahith le 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]);
disp('No purpose selected.');
For more details on using the “listdlg” function, please follow the below provided MathWorks Documentation link


En savoir plus sur Logical dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by