0

I have a structure array as follows:

configStruct = 
20x1 struct array with fields:
    type
    id
    manufacturer
    model

How do I find the index of an element with fields, for example:

            type: 'Mainframe'
              id: '5'
    manufacturer: 'IBM'
           model: 'z14'

I've figured out how to find the indices of a structure using only one criteria:

find(strcmp({configStruct.type},'Mainframe'))

Scaling it up to two criteria would be something like:

find(strcmp({configStruct.type},'Mainframe') & strcmp({configStruct.id},'5'))

Scaling that up will get pretty cumbersome if I continue to add fields and criteria for those fields.

2 Answers 2

2

Just loop over it.

LogIdx = arrayfun(@(n) isequal(configStruct(n),Element), 1:numel(configStruct));
%where Element is the struct that you want to find in configStruct

The above line gives logical indices. If linear indices are required, further use:

LinIdx = find(LogIdx);
Sign up to request clarification or add additional context in comments.

1 Comment

In this case, Element.id = '5'; Element.type = 'Mainframe'; Element.manufacturer = 'IBM'; Element.model = 'z14';
0

I'm not aware of any native function would do what you are asking, but I recommend you to break all kinds of strcmp into sub-functions:

global configStruct
find(isType('Mainframe') & isId('5'));

function val = isType(type)
global configStruct
val = strcmp({configStruct.type}, type);
end

function val = isId(id)
global configStruct
val = strcmp({configStruct.type}, id);
end

Comments

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.