Avoid Repeated Searches for Objects
When you search for handles, MATLAB® must search the object
hierarchy to find matching handles, which is time-consuming. Saving
handles that you need to access later is a faster approach. Array
indexing is generally faster than using
This code creates 500 line objects and then calls
figure ax = axes; for ix=1:500 line(rand(1,5),rand(1,5),'Tag',num2str(ix),'Parent',ax); end drawnow; for ix=1:500 h = findobj(ax,'Tag',num2str(ix)); set(h,'Color',rand(1,3)); end drawnow;
A better approach is to save the handles in an array and index
into the array in the second
figure ax = axes; h = gobjects(1,500); for ix = 1:500 h(ix) = line(rand(1,5),rand(1,5),'Tag',num2str(ix),'Parent',ax); end drawnow; % Index into handle array for ix=1:500 set(h(ix),'Color',rand(1,3)); end drawnow
Limit Scope of Search
If searching for handles is necessary, limit the number of objects to be searched by specifying a starting point in the object tree. For example, specify the starting point as the figure or axes containing the objects for which you are searching.
Another way to limit the time expended searching for objects
is to restrict the depth of the search. For example, a
restricts the search to the objects in a specific handle array.
For more information, see Find Objects