필터 지우기
필터 지우기

How to search through nested property's values of the class array?

조회 수: 4 (최근 30일)
Oleksandr Kozar
Oleksandr Kozar 2016년 3월 18일
답변: Oleksandr Kozar 2016년 5월 19일
Assume we have array of class objects. Our class is a subclass of the handle class. We can use findobj function to find handle objects by first-level property's value. It's ok.
For example, a class structure:
className
|——property1
|——property2
|——structproperty1
———|——innerProperty1
———|——innerProperty2
A findobj function will return results only if every structure field is filled. Below is an example.
b = 1x1234 className;
structToFind = struct ('innerProperty1', 10, 'innerProperty2', 20);
H = findobj (b, 'structproperty1', structToFind);
I want to search className handle objects by innerProperty1. Is there any way to make it possible?

답변 (2개)

Oleksandr Kozar
Oleksandr Kozar 2016년 5월 19일
Thank you, Tejas. Fortunately I found an easier solution. I used `findobj(H,'-function',fh)`. We can use function handle for filtering the object. So for this test example it should be something like this:
b: 1x1234 className
H = findobj (b, '-function', 'structproperty1', @(x) isfield(x, 'innerProperty1') );

Tejas
Tejas 2016년 3월 22일
‘findobj’ matches the properties with in an object to find a matching object with the specified property.
As you are looking to match inner properties, I am not sure ‘findobj’ can be helpful. If you are open to implementing your own way of achieving this then the following might be useful.
Let us suppose your objects belong to class that looks like this:
classdef WithStruct < matlab.mixin.SetGet
properties
struct_prop
end
methods
function obj = WithStruct
val1 = randi(100); val2 = randi(50);
obj.struct_prop.inner1 = val1;
obj.struct_prop.inner2 = val2;
end
end
end
I am using ‘ structfind ’ from File Exchange to find the matching object. You could implement something that does this which is more tailored to your requirements.
>> h = [WithStruct WithStruct];
>> structfind(cell2mat(get(h,'struct_prop')),'inner1',49)
ans =
1 % index of the matching object in ‘h’ array

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by