检测MATLAB表中的单元条目

时间:2022-12-03 15:57:14

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

我有一个Matlab表(新的'Table'类),让我们称之为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.

由于某种原因,我没有使用A.Properties.class,我似乎无法在其上调用iscell。

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

什么是“Matlab”这样做的方式?我是否必须遍历表格的每一列才能找出它的类?

2 个解决方案

#1


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 -

运行#1 -

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

Run #2 -

跑#2 -

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

Run #3 -

跑#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

#2


You could test with iscell(A.Var2) if the second variable is of type cell. More generally, you could reference columns by their index:

如果第二个变量是cell类型,则可以使用iscell(A.Var2)进行测试。更一般地说,您可以通过索引引用列:

for k = 1 : width(A)
    disp(iscell(A.(k)))
end

#1


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 -

运行#1 -

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

Run #2 -

跑#2 -

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

Run #3 -

跑#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

#2


You could test with iscell(A.Var2) if the second variable is of type cell. More generally, you could reference columns by their index:

如果第二个变量是cell类型,则可以使用iscell(A.Var2)进行测试。更一般地说,您可以通过索引引用列:

for k = 1 : width(A)
    disp(iscell(A.(k)))
end