different behaviour when concatenating table colums

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

Answers (1)

Voss
Voss on 23 Dec 2021
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.

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!