0

同様のフィールド名を持ついくつかの構造体配列(structure1、structure2、structure3、...など)があります。すべての構造をスキャンして、最初のフィールドが5(Field1 == 5)の構造のみを返します。私はこれまでにこのコードを持っています、

for k=1:3
    s=sprintf('Structure%d',k)
    Structure=load(s)
    idx=cellfun(@(x) x==5, {Structure.Field1})
    out=Structure(idx)
    v{k}={Structure.Field1}
end

しかし、それは私にこのエラーを与えます:

Reference to non-existent field 'Field1'. 

誰かがここで何が悪いのか指摘できますか?

ありがとう

4

2 に答える 2

0
for k=1:3
    s=sprintf('Structure%d',k)
    Structure=load(s)
    eval(['newStructure(k)=Structure.' s]);
    idx(k)=cellfun(@(x) x==5, {newStructure(k).Field1})
end
%extract the structures from newStructure which have 1 in idx
out=newStructure(idx); %idx should be a logical array
for i=1:size(out,2)
    v(i)=out(i).Field1;
end

これは完全に機能するはずです。

于 2013-02-24T09:08:05.993 に答える
0

'Field1'保存された構造の一部にフィールドが含まれていないように見えます。
その場合、別の方法を試してみるとよいでしょう。
最初に関数を定義します (m ファイルで)

function res = iff( cond, true_case, false_case )
%
% conditional execution of two function handles
% 
% true_case and false_case are function handles expecting no inputs
%
if cond
   res = true_case();
else
   res = false_case();
end

この関数を取得したら、cellfun

idx = cellfun( @(x) iff( isfield(x, 'Field1'), @() x.Field1 == 5, @() false), Structure );
于 2013-02-24T09:09:00.170 に答える