find a number in a structure in matlab

Assuming you have a structure array, you could use a for loop with conditional statements to search for a specific number. Here is an example code:

main.m
% Example structure array
s(1).field1 = 42;
s(1).field2 = 'hello';
s(2).field1 = 33;
s(2).field2 = 'world';

% Number to search for
number_to_find = 33;

% Loop through structure array to find number
for i = 1:length(s)
    if s(i).field1 == number_to_find
        disp(['Number found in structure at index ' num2str(i)]);
        % If you want to exit the loop after finding the first occurrence:
        % break;
    end
end
427 chars
18 lines

This code will loop through the structure array s, and check if the field1 value of each struct matches the number_to_find. If a match is found, it will display the index of that struct in the array (in this case, it would display "Number found in structure at index 2", since s(2).field1 is equal to number_to_find).

related categories

gistlibby LogSnag