Extract length of data in a structure

17 ビュー (過去 30 日間)
Stefanie
Stefanie 2014 年 1 月 24 日
コメント済み: Andrei Bobrov 2014 年 1 月 26 日
I have a structure with arrays of different size inside:
STRUC(1).meas=ones(101,2)
STRUC(2).meas=ones(34,2)
STRUC(3).meas=ones(33,2)
STRUC(4).meas=ones(101,2)
now I want to get an index, which shows me all entries, where the size of STRUC.meas is > 100:
idx=[1 4]
so that I can filter all entries of STRUC with this condition (incl. other fields) with
STRUC(idx)
As I understand structfun is only for scalar structs... is there an easy and elegant way (if possible without for-loop)?

採用された回答

Jos (10584)
Jos (10584) 2014 年 1 月 26 日
Still no need for a conversion:
a(1).b=ones(4,8);
a(2).b=ones(8,1);
a(3).b=ones(3,2);
a(4).b=ones(7,1);
lengths = arrayfun(@(x) size(a(x).b,1), 1:numel(a))
keepA = a(lengths>5)
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2014 年 1 月 26 日
+1

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

その他の回答 (2 件)

Jos (10584)
Jos (10584) 2014 年 1 月 24 日
編集済み: Jos (10584) 2014 年 1 月 24 日
No need for conversion to cell array. Use STRUCTFUN on the structure directly:
a(1).b=ones(4,8);
a(2).b=ones(8,1);
a(3).b=ones(3,2);
a(4).b=ones(7,1);
lengths = structfun(@(x) size(x,1), a)
keepA = a(lengths>5)
  2 件のコメント
Patrik Ek
Patrik Ek 2014 年 1 月 24 日
Sorry, this will not work on a struct array. structfun works only on scalar structs, thus the struct array need to be converted to a format matlab can handle.
Jos (10584)
Jos (10584) 2014 年 1 月 26 日
Yes, you're right. But there is really no need for a conversion! See my 2nd answer.

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


W. Owen Brimijoin
W. Owen Brimijoin 2014 年 1 月 24 日
If you are intent on avoiding a for-loop, then you could try converting the structure to a cell array and then using a cell function. An example would look something like this:
idx = cellfun(@length,struct2cell(STRUC(:)))>100
idx =
1 0 0 1

カテゴリ

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