フィルターのクリア

Select specific data from nested struct ?

1 回表示 (過去 30 日間)
azdoud youssef
azdoud youssef 2018 年 1 月 17 日
コメント済み: Walter Roberson 2018 年 1 月 19 日
Hi, I have the following structure: data
I need to select those where data.name = t1 and t6
and this case too :
I need to select those where data.name = t1 and t6 and data.type= 'aac'
Glad for any help
data(1:6) = struct('name', '', 'type', '', 'value', zeros(1,1));
data(1).name= 't1'; data(1).type= 'aac';data(1).value=0.569;
data(2).name= 't2'; data(2).type= 'rta';data(2).value=0.657;
data(3).name= 't1'; data(3).type= 'vb';data(3).value=0.98;
data(4).name= 't6'; data(4).type= 'aac';data(4).value=0.451;
data(5).name= 't2'; data(5).type= 'ed';data(5).value=0.354;
data(6).name= 't1'; data(6).type= 'aab';data(6).value=0.846;

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 17 日
mask = ismember({data.name}, {'t1', 't6'});
selected_data = data(mask);
  6 件のコメント
azdoud youssef
azdoud youssef 2018 年 1 月 19 日
I tried with float value, however, I got errors
mask = ismember({data.value}, {0.98});
selected_data2 = data(mask);
Error using cell/ismember
Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string.
Walter Roberson
Walter Roberson 2018 年 1 月 19 日
mask = ismembertol([data.value], [0.98]);
Notice the switch to ismembertol(). ismember() is for exact (bit-for-bit) comparisons, but calculated values are seldom bit-for-bit identical to floating point constants because of round-off error. http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F . ismembertol() is a convenient way to test "close too".
You should also be considering a range test:
temp = [data.value];
mask = (temp = 0.97) & (temp < 0.99);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by