0

I have a structure in MATLAB with over one hundred data entries per field.

struct.p = [1 2 3 4 ...]

I want to be able to search for a field by typing in a data entry. For example, typing 2 will tell me that the field is 'p'.

I've tried combining isfield and find which hasn't worked.

find(isfield(p(9)))
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58

1 Answers1

0

If A is the structure array and k is the value to be searched in all fields then:

ind = structfun(@(x) any(x==k), A);  %Loop over all structure fields to search k 
fn = fieldnames(A);                  %Name of all the fields
kfield = fn(ind);                    %Name of those fields which have k

struct is a builtin function, don't shadow it with your structure.
Rename your structure to something else.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58