I'm trying to understand your tables command bugs

1 Ansicht (letzte 30 Tage)
Stephen Wilkerson
Stephen Wilkerson am 20 Feb. 2020
Beantwortet: Steven Lord am 20 Feb. 2020
% 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?

Akzeptierte Antwort

Steven Lord
Steven Lord am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by