필터 지우기
필터 지우기

different behaviour when concatenating table colums

조회 수: 2 (최근 30일)
Thomas Laubscher
Thomas Laubscher 2021년 3월 31일
답변: Voss 2021년 12월 23일
The two lines behave differently:
tmp = [all_table.('SpO2_FiO2');myTable.('SpO2_FiO2')]; % this works
tmp = [all_table(:,52);myTable(:,52)]; % this gives error 'Cannot concatenate the table variable 'SpO2_FiO2' because it is a cell in one table and a non-cell in another.'
52 is the column index to the column named ''SpO2_FiO2". The type of this column is double in both tables, all_table and my_Table.

답변 (1개)

Voss
Voss 2021년 12월 23일
If both tables' SpO2_FiO2 columns are actually doubles, then both ways of concatenating do the same thing:
SpO2_FiO2 = (1:3).';
all_table = table(SpO2_FiO2)
all_table = 3×1 table
SpO2_FiO2 _________ 1 2 3
myTable = table(SpO2_FiO2)
myTable = 3×1 table
SpO2_FiO2 _________ 1 2 3
tmp = [all_table.('SpO2_FiO2');myTable.('SpO2_FiO2')]
tmp = 6×1
1 2 3 1 2 3
tmp = [all_table(:,1);myTable(:,1)]
tmp = 6×1 table
SpO2_FiO2 _________ 1 2 3 1 2 3
If instead one table's SpO2_FiO2 column is a cell array, then you get behavior consistent with what you observe:
SpO2_FiO2 = (1:3).';
all_table = table(SpO2_FiO2)
all_table = 3×1 table
SpO2_FiO2 _________ 1 2 3
SpO2_FiO2 = num2cell(SpO2_FiO2);
myTable = table(SpO2_FiO2)
myTable = 3×1 table
SpO2_FiO2 _________ {[1]} {[2]} {[3]}
tmp = [all_table.('SpO2_FiO2');myTable.('SpO2_FiO2')]
tmp = 4×1 cell array
{3×1 double} {[ 1]} {[ 2]} {[ 3]}
tmp = [all_table(:,1);myTable(:,1)]
Cannot concatenate the table variable 'SpO2_FiO2' because it is a cell in one table and a non-cell in another.
In the second case, the line:
tmp = [all_table.('SpO2_FiO2');myTable.('SpO2_FiO2')];
does not generate an error, but it doesn't necessarily 'work' because it doesn't necessarily do what you want. It gives a 4-by-1 cell array where the first element is a 3-by-1 vector of doubles and the 2nd, 3rd, and 4th elements are all scalar doubles, as opposed to a 6-by-1 vector of doubles, which is what you get either way you do the concatenation when both columns are actually doubles.

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by