Writing a script to create a table based on user input with row names and columns

I am writing a script that takes in user input for creating a table, but I cannot seem to find out how to prompt the user for row and column names based on how many rows and columns they select.
The image shows an example of what kind of table I would like to create.
This is what I have so far:
row = input('Enter number of rows: ');
col = input('Enter number of columns: ');
for i = 1:row
promptRow = ['Row ' num2str(i) ' Name: '];
x = input(promptRow, 's');
for j = 1:col
promptCol = ['Column ' num2str(i) ' Name: '];
y = input(promptCol, 's');
str = ['Enter element in row ' num2str(i) ', col ' num2str(j) ': '];
A(i,j) = input(str);
end
end
A;
T = array2table(A,...
'VariableNames',{x},...
'RowNames', {y});

 Réponse acceptée

KL
KL le 7 Nov 2017
Modifié(e) : KL le 7 Nov 2017
Do it like this,
row = input('Enter number of rows: ');
col = input('Enter number of columns: ');
A = zeros(row,col);
now before asking the user to input values of the matrix, ask for the row and column names,
rowNames = input('Enter names of rows separated by a space: ',s);
rowNames = strsplit(rowNames);
colNames = input('Enter names of column separated by a space: ',s);
colNames = strsplit(colNames);
for i = 1:row
for j = 1:col
str = ['Enter element in row ' num2str(i) ', col ' num2str(j) ': '];
A(i,j) = input(str);
end
end
then create the table,
T = array2table(A,'VariableNames', colNames,'RowNames', rowNames);
Hope it helps.

4 commentaires

I'm all new to this; everything works fine, apart from the total for each row and column. My error is that the table isn't created if I don't specify the row and column name for the totals. E.g if I want a 2x2 table, I still have to put 3 values for row and columns, with the last one being 'Total'
row = input('Enter number of rows: ');
col = input('Enter number of columns: ');
A = zeros(row + 1,col + 1);
rowNames = input('Enter names of rows separated by a space: ', 's');
rowNames = strsplit(rowNames);
colNames = input('Enter names of column separated by a space: ','s');
colNames = strsplit(colNames);
for i = 1:row
for j = 1:col
str = ['Enter element in row ' num2str(i) ', col ' num2str(j) ': '];
A(i,j) = input(str);
end
end
A;
A(end,1:col) = sum(A(1:row,1:col),1);
A(1:row,end) = sum(A(1:row,1:col),2);
A(end,end) = sum(sum(A));
T = array2table(A,'VariableNames', colNames,'RowNames', rowNames);
I know it's something as simple as adding total after colNames and rowNames in the last line, but not sure what the syntax is
oh no, do it like this. Enter only the row and column names.. and later when you create a table, do it like,
T = array2table(A,'VariableNames',[colNames {'Total'}],'RowNames',[rowNames, {'Total'}]);
Thank you! Everything works perfectly now, just made a small change, instead of
A(end,end) = sum(sum(A));
I did
A(end,end) = sum(A(end,1:col));
As the former calculated the total of the columns and rows, meaning that it was double what it should have been.
Oh yes. In the original thread, initially I remember suggesting something like, sum(sum(1:row,1:col)), but anyway, it's all the same. Glad it worked:)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by