Reputation: 738
I created an object in MATLAB by using my own class my_class
like this
car = my_class();
with
classdef my_class < handle
properties
color = 'red';
end
methods
function obj = my_class()
% ...
end
end
end
Now I am trying to find my object by its class (my_class
) or by properties (color
). But findall
or findobj
always return an empty matrix, whatever I am doing. Do you have any clue? Thanks.
EDIT I need something like this:
car1 = my_classA();
car2 = my_classA();
house1 = my_classB(); ... house25 = my_classB();
tree1 = my_classC(); ... tree250 = my_classC();
In my code, I can not refer to the names of the handles (like car2.color
), because I have many different objects and I want to search for them by a function, that looks like the following one:
loop over all objects (maybe with findobj/findall without knowing object name/handle)
if object is of class `my_classA`
get handle of `my_classA`
change `color`
else if object is of class `my_classB`
get handle of `my_classB`
do something ...
end
end
Upvotes: 2
Views: 72
Reputation: 38032
I think you just want this:
% Create example array of objects
A(20) = object;
[A([3 14 17]).color] = deal('blue');
% Get those objects which are red, and change to orange
[A(strcmp({A.color}, 'red')).color] = deal('orange');
I have to admit, findobj
would have been much better to read. But that only works on graphics handles as far as I'm aware, so you'd have to overload it for your class.
And that overloaded function, would contain something similar to this.
EDIT as noted by Navan, this works:
B = findobj(A, 'color', 'red');
[B.color] = deal('orange');
seems to be faster than the strcmp
method, too.
Upvotes: 2