If no graphics objects are added to or removed from a figure, does findobj always generate the list in the same order?
Show older comments
Hopefully my question is clear from the title, but I'll try to clarify what I mean.
Suppose I have a figure full of graphics objects and I want to gather all of these objects to perform some kind of function on each one.
hf = figure;
axes;
uicontrol;
hObjects = findobj(hf);
for i = 1:length(hObjects)
foo(hObjects(i));
end
Now suppose this array of graphics objects goes out of scope but later on, I want to perform some other function on each of these objects.
hf = gcf;
hObjects = findobj(hf);
for i = 1:length(hObjects)
bar(hObjects(i));
end
If I haven't added or removed any graphics objects from the figure (nor changed their Type), am I guaranteed the same list of objects from findobj? For example, if bar is the inverse function to foo in the sense that foo(bar(hObject)) == hObject is true for a given graphics object hObject, does the above code revert all my graphics objects to their original state in the end?
Thanks in advance for your answers!
Accepted Answer
More Answers (1)
Jogesh Kumar Mukala
on 14 Jul 2021
Hi,
If no graphics objects are added to or removed from a figure, the findobj always generates the list in the same order. The findobj always returns all the objects in a hierarchical manner.
By observing the example you provided, it seems you have a doubt about the order of the list after applying some function over graphics objects. To clarify regarding applying some functions over graphics objects, you may refer below attached code. You can refer isa to understand the function in the code.
plot(rand(5))
h = findobj;
% Generates the list before applying the function
h
for i =1:length(h)
foo(h(i));
end
% Generates the list after applying the function
k= findobj;
k
function foo(x);
if(isa(x, 'matlab.ui.Root'))
% Change the units of graphic object root as it doesnot have color property
x.Units = 'pixels';
elseif(isa(x,'matlab.graphics.chart.primitive.Line'))
% Change color of the graphic objects lines
x.Color = [1 1 0];
else
% Change color of the graphic object figure
x.Color = [1 1 1];
end
end
2 Comments
fig = figure(1);
ax1 = axes(fig, 'tag', 'ax1');
ax2 = axes(fig, 'tag', 'ax2');
findobj(fig)
uistack(ax1,'top')
drawnow()
findobj(fig)
This example proves that uistack() can affect the order in which findobj() retrieves objects.
Jogesh Kumar Mukala
on 14 Jul 2021
Thanks for pointing it out Walter.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
