Search code examples
matlabcell-array

Detect cell entries in MATLAB Table


I have a Matlab table (the new 'Table' class), let's call it A:

A=table([1;2;3],{'A';'B';'C'})

As you can see, some of the columns are double, some are cell.

I'm trying to figure out which ones are cells.

For some reason, there is no A.Properties.class I can use, and I can't seem to call iscell on it.

What's the "Matlab" way of doing this? Do I have to loop through each column of the table to figure out its class?


Solution

  • One approach -

    out = cellfun(@(x) iscell(getfield(A,x)),A.Properties.VariableNames)
    

    Or, a better way would be to access the fields(variables) dynamically like so -

    out = cellfun(@(x) iscell(A.(x)), A.Properties.VariableNames)
    

    Sample runs:

    Run #1 -

    A=table([1;2;3],{4;5;6})
    A = 
        Var1    Var2
        ____    ____
        1       [4] 
        2       [5] 
        3       [6] 
    out =
         0     1
    

    Run #2 -

    >> A=table([1;2;3],{'A';'B';'C'})
    A = 
        Var1    Var2
        ____    ____
        1       'A' 
        2       'B' 
        3       'C' 
    out =
         0     1
    

    Run #3 -

    >> A=table([1;2;3],{4;5;6},{[99];'a';'b'},{'m';'n';'p'})
    A = 
        Var1    Var2    Var3    Var4
        ____    ____    ____    ____
        1       [4]     [99]    'm' 
        2       [5]     'a'     'n' 
        3       [6]     'b'     'p' 
    >> out
    out =
         0     1     1     1