1

I have a struct with about thousands of field names and corresponding values. The field names are mixed in capitalization but are unique as far as name is concerned. I need to look up for values. I know the field name for which I am looking for value but I do not know the exact case of the letters.

A sample code is:

A = struct();
A.cat = 14;
A.Dog = 11;
A.COw = 13;

How do I look up what value is for cow in struct A ?

Thanks

Suever
  • 64,497
  • 14
  • 82
  • 101
Zanam
  • 4,607
  • 13
  • 67
  • 143

1 Answers1

4

You can create a case-insensitive version getfield like this:

function value = getfieldi(S,field)
    names   = fieldnames(S);
    isField = strcmpi(field,names);  

    if any(isField)
      value = S.(names{isField});
    else
      value = [];
    end
end
TroyHaskin
  • 8,361
  • 3
  • 22
  • 22