MATLAB: I’m trying to understand your tables command bugs

crazy stuffMATLAB and Simulink Student Suite

% Why does this happen?
A = randi([0 20],5, 5)
table(A,'RowNames',{'a' 'b' 'c' 'd' 'e'}) % doesn't work
table(A(:,1)','RowNames',{'a' 'b' 'c' 'd' 'e'},'VariableNames',{'a'}) % Doesn't work
table(A(1,:)','RowNames',{'a' 'b' 'c' 'd' 'e'},'VariableNames',{'a'}) % Works
% Who's writing this software?

Best Answer

What specifically does "doesn't work" mean? If you receive errors when you try running those commands, please show the full and exact text of the error messages you receive.
The first one works when I run it in release R2019b and creates the table array I expected, with five rows and one variable. If you expected it to have five rows and five variables, use array2table instead.
array2table(A, 'RowNames', {'a','b','c','d','e'}, 'VariableNames', {'f', 'g', 'h', 'i', 'j'})
The second one correctly throws an error. A(:, 1) is a 5-by-1 vector so A(:, 1)' is a 1-by-5 vector. A table with one row can't have five RowNames. If you specified just one RowNames entry, it would work and create a table with one row and one variable.
table(A(:,1)','RowNames',{'a'},'VariableNames',{'b'})
If you expected it to create a table with one row and five variables, again use array2table instead.
array2table(A(:, 1)', 'RowNames', {'row1'}, 'VariableNames', {'a','b','c','d','e'})
The third one also works for me, creating a table with five rows and one variable.