从结构数组中提取特定字段的所有值

extract all values of a specific field from a struct array

如果我有一个像

这样的数组
 A = [
   struct( 'uid', 165215, 'type', 8, 'data', [0,3,16388,17523,12,225,225,280,242,223,256,266,261,226,225,259,210]);
   struct( 'uid', 196334, 'type', 2, 'data', [0,96,398,359,350,4,416,406,450,39]);
   % ...
   struct( 'uid', 173261, 'type', 8, 'data', [0,13,5081,5658,48]);
 ];

有没有办法将所有 'type' 成员提取到自己的 Nx1 矩阵中? 类似于:

b = A(:).type; % this only returns "b = 8"

或更复杂的对成员应用逻辑运算符并获得答案向量:

I = A(:).type==1;

这抛出:

error: binary operator '==' not implemented for 'cs-list' by 'scalar' operations

索引操作就像您使用的那样 returns cs-list(在 matlab 中称为逗号分隔变量)。要将其转换为数组或元胞数组,请将相应的括号括起来:

 b = [A(:).type] ;

上面的行创建了一个数组,如果您需要一个单元格,在其他情况下请改用 {}。您将需要它来索引数据。