check if a strucutre contains a certain number in matlab

Assuming you want to check if a field named myField in your structure myStruct contains a certain number (let's say myNumber), you can use the following code:

main.m
if isfield(myStruct,'myField') && any([myStruct.myField] == myNumber)
    disp('myField contains myNumber');
else
    disp('myField does not contain myNumber');
end
165 chars
6 lines

The code first checks if the field myField exists in myStruct using isfield function. If the field exists, it checks if any element in the array of myField values is equal to myNumber using any function. If any element is found to be equal to myNumber, the code displays a message saying that myField contains myNumber. Otherwise, it displays a message saying that myField does not contain myNumber.

Note that this code assumes that the field myField contains an array of numbers in the structure myStruct. If myField contains a single number instead, you can simplify the code as follows:

main.m
if isfield(myStruct,'myField') && myStruct.myField == myNumber
    disp('myField contains myNumber');
else
    disp('myField does not contain myNumber');
end
158 chars
6 lines

related categories

gistlibby LogSnag