Home > front end >  How to find objects with specific properties(another objects with specific properties) in Matlab?
How to find objects with specific properties(another objects with specific properties) in Matlab?

Time:08-09

For example, an object 'student' with a property 'test' , and 'test' is also an object with a property 'score'.

classdef student < handle

    properties
        name
        test
    end

    methods


        function obj = student(name)
            obj.name = name;
            obj.test = test();
        end
    end
end
classdef test < handle

    properties
        content
        score
    end
end
student_arr=[student('A') student('B')]
student_arr(1).test.score=100
student_arr(2).test.score=80

I want to find students whose score of test is 100. I use function findobj

findobj([student_arr.test],'score',100)

ans = 

  test with properties:

    content: []
      score: 100

It return a test array, not a student array.

But if I try to find it in the student_arr

findobj(student_arr,'score',100)

ans = 

  0×0 student array with properties:

    name
    test

It return a 0*0 student array, because 'score' is not a property of student.

The question is, if an object's property is another object, how to find the former based on properties of latter?

CodePudding user response:

It is a bit undocumented, but you can use findobj with an additionnal function to specify the search:

H = findobj (student_arr, '-function', @(x) x.test.score == 100 );
  • Related