If no graphics objects are added to or removed from a figure, does findobj always generate the list in the same order?
3 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Walter Roberson
2021-7-14
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?
No.
uistack() can change the order of objects.
Making a figure current (such as by clicking on it) changes the order of objects -- I tested this for figures
I would not be at all astonished if making an object current by clicking on it, had the effect of raising it inside its container -- and likewise, its container inside the container of the container, and so on. Clicking a line probably moves the line to the top of its group, and moves the axes that the line is in to the top of its uipanel, and the uipanel to the top of its figure, and the figure to the top of the active figures. I have not tested this (only tested with figures), but it would not astonish me.
5 个评论
Walter Roberson
2021-7-22
You can save the handles that you got back from findObj somewhere, so that you could be sure of getting the exact handles back.
However... since you are setting all of the uicontrol except perhaps one button, then you do not care about order. Just set them all to disable, then set the one button to enable; to restore, set them all to enable (it won't matter that the one button was already set to enable.)
更多回答(1 个)
Jogesh Kumar Mukala
2021-7-14
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 个评论
Walter Roberson
2021-7-14
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!