Concatenate cell with numeric array in tables

I have a ridiculously simple problem that I cannot solve. I have two tables and I want to concatenate them, but one contains a column with a cell array and one with a numeric array.
a = table([1; 2; 3]);
b = table({[1 2]; [3 4]})
c = [a; b];
This immediately produces an error because I am trying to concatenate a cell with a non-cell type. But if I just try to concatenate these variables outside a table, it works fine, recognizing that the result should be a cell array. There seems to be no way of telling matlab that I want it to convert a's variable to a cell array.
The actual problem is somewhat more complex - it's easy enough to solve this in the current case by just removing the variables from the table and concatenating them, then making a new table. But in the actual use case the tables have up to 50 variables, and it's unknown at runtime which of them are causing the problem.
Suggestions?

 Respuesta aceptada

Robert U
Robert U el 30 de Oct. de 2019
Hi Marcus Watson,
an straight-forward solution would be checking the variable types stored in the table. In case they don't match change the numerical one to cell array.
a = table([1; 2; 3]);
b = table({[1 2]; [3 4]})
if ~cellfun(@isempty,{a{:,vartype('numeric')}}) && cellfun(@isempty,{b{:,vartype('numeric')}})
a = table({a{:,vartype('numeric')}});
elseif cellfun(@isempty,{a{:,vartype('numeric')}}) && ~cellfun(@isempty,{b{:,vartype('numeric')}})
b = table({b{:,vartype('numeric')}});
end
c = [a; b];
In case there are anyway no numerical values to concatenate you can change all tables that are containing numerical values to cells.
Kind regards,
Robert

5 comentarios

Marcus Watson
Marcus Watson el 30 de Oct. de 2019
That does at least allow the concatenation, which is a step in the right direction, but it converts A into a single cell in the resulting concatenated table (and the cell contains the vector). What I need is the ability to have every row of A and B be a row in C. Unfortunately I'm not sure that's possible without looping through each value, one at a time.
Robert U
Robert U el 30 de Oct. de 2019
How to deal with rows having a different number of columns?
I came accross the same wish while thinking about a solution. In your example a is 3x1 double whereas b is 2x2. What format is expected as output? Matlab does not even support that kind of concatenation with numerical data types.
Kind regards,
Robert
Marcus Watson
Marcus Watson el 30 de Oct. de 2019
No, b is a 2x1 cell vector. So I’m hoping for the resulting concatenation to be a 5x1 cell.
a = table([1; 2; 3]);
b = table({[1 2]; [3 4]});
if cellfun(@isempty,{a{:,vartype('cell')}}) && ~cellfun(@isempty,{b{:,vartype('cell')}})
a = table(num2cell(a{:,vartype('numeric')}));
elseif ~cellfun(@isempty,{a{:,vartype('cell')}}) && cellfun(@isempty,{b{:,vartype('cell')}})
b = table(num2cell(b{:,vartype('numeric')}));
end
c = [a; b];
Marcus Watson
Marcus Watson el 1 de Nov. de 2019
Apologies my previous comment's code did not actually do the same as yours. Yours is the only one that I've seen work.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Oct. de 2019

Comentada:

el 1 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by