I'm trying to understand your tables command bugs

2 views (last 30 days)
% 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?

Accepted Answer

Steven Lord
Steven Lord on 20 Feb 2020
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.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!