MATLAB Answers

cell2table behaviour on numerical arrays of different sizes

5 views (last 30 days)
Kouichi C. Nakamura
Kouichi C. Nakamura on 27 May 2016
Edited: Kouichi C. Nakamura on 27 May 2016
This isn't actually a question. Rather, this is a feature request.
In the documentation of cell2table , it reads "If the cell contents have compatible sizes and types, then cell2table vertically concatenates the contents of the cells in each column of C to create each variable in T. If the cell contents have different sizes or incompatible types, then the corresponding variable in the table, T, is a column of cells."
K>> A = {[1 2 3 4 5],2,[1 2 3];...
[6 7 8 9],5,4}
A =
[1x5 double] [2] [1x3 double]
[1x4 double] [5] [ 4]
K>> B = cell2table(A)
B =
A1 A2 A3
____________ __ ____________
[1x5 double] 2 [1x3 double]
[1x4 double] 5 [ 4]
As you can see, cell2table does what it promised to do, but it's not quite good for me. Say, the cell array A holds the results of computations and sample number for each cell of A is different. I believe this is not rare practice. So, I would like to see something like this:
B2 =
A1 A2 A3
____________ __ ____________
[1x5 double] [2] [1x3 double]
[1x4 double] [5] [ 4]
Otherwise, depending on the sample sizes, the data type of variables can be numeric or cell, making code highly error-prone. In this example, variables in A2 are just happened to be a scalar, and thus it is stored as double in B. It's kind of unpredictable. This turned out to be a major problem, when I replaced a cell array in my code with a table by using cell2table for better readability.
Of course I can "manually" amend B after cell2table (in fact I had to), by checking the class of resultant variables (columns). But wouldn't it be useful if there were an optional parameter/value pair that defines the behaviour of cell2table for all the columns or for each column?
I can think of a syntax like this:
K>> B3 = cell2table(A,'NonuniformOutput',true)
which returns all the columns as cell arrays
or
K>> B4 = cell2table(A,'NonuniformOutput',[false,true,true])
where you can specify the action column by column manner. I think I can write a wrapper function of cell2table to achieve this, but just thought, if it is officially implemented, it's better for many.

  0 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by