I'm trying to understand your tables command bugs

조회 수: 1 (최근 30일)
Stephen Wilkerson
Stephen Wilkerson 2020년 2월 20일
답변: Steven Lord 2020년 2월 20일
% 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?

채택된 답변

Steven Lord
Steven Lord 2020년 2월 20일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by