0

I have some structure arrays (like structure1, structure2, structure3,...) with similar field names. I want to scan through all the structures and return only those whose first field is 5 (Field1==5). I have this code so far,

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

but it gives me this error:

Reference to non-existent field 'Field1'. 

Can someone please point out whats wrong here?

Thanx

2 Answers 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

This should work perfectly.

Sign up to request clarification or add additional context in comments.

6 Comments

There is a 'Field1' in every structure. The problem seems to be that Structures are stored in the workspace as Structure.Structure1.Field1. It doesn't work if I write {Structure.Structure(k).Field1} in the cellfun. Is there a way around it?
Why not extract out the structure into different variable as: newStruct=Structure.Structure1 and then proceed?
It works if I do it manually for each structure (Structure.Structure1, Structure.Structure2, ...) but how can I do that dynamically in the same loop?
It seems simple enough to do, but I am still struggling with it (I am relatively new at this). I just want to scan through multiple structures with same fields and save the structures which meets my search criteria into a separate vector. Is it even possible?
Thanx a lot. This works like a charm and returns the correct idx values (in 0 and 1). But when it comes out of the loop to out=newStructure(idx), it gives an error: "Index exceeds matrix dimensions". I'm sure its just a small glitch, but what does it mean?
|
0

It appears as if some of your saved structures do not have 'Field1' as a field in them.
In that case, you might want to try something else.
First define a function (in m-file)

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

Once you have this function you can use it in the cellfun

idx = cellfun( @(x) iff( isfield(x, 'Field1'), @() x.Field1 == 5, @() false), Structure );

2 Comments

There is a 'Field1' in every structure. The problem seems to be that Structures are stored in the workspace as Structure.Structure1.Field1. It doesn't work if I write {Structure.Structure(k).Field1} in the cellfun. Is there a way around it?
It seems simple enough to do, but I am still struggling with it (I am relatively new at this). I just want to scan through multiple structures with same fields and save the structures which meets my search criteria into a separate vector. Is it even possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.