2015-12-02 138 views
2

提取特定字段的所有值,如果我有像從結構陣列

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]); 
]; 

數組有沒有一種方法來提取所有的「類型」的成員到自己的NX1矩陣? 類似:

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

或更復雜的成員適用的邏輯運算符和得到答案的一個載體:

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

,這將引發:

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

回答

3

像一個的索引操作你使用的是返回一個cs-list(在matlab中稱爲逗號分隔變量)。將其轉換爲一個數組或單元陣列,使相應的括號它:

b = [A(:).type] ; 

線之上創建一個數組,如果你需要在其它情況下,電池使用{}代替。你將需要它來索引數據。