Newer Matlab versions assign sequence of plot handle double-values in a code-dependent way
Show older comments
Hello,
I have some codes that rely on the sequence of numerical (double) values of plot handles to process them in the order they were plotted.
In older versions, it seems they were always in "chronological" order, i.e. double(hp(first)) < double(hp(second)). (Indeed, in the "old days", hp=plot... only returned this numerical handle value)
In newer versions (here using R2021a), this seems to have changed. As per the following example:
Np=3;
figure(1)
clf
for ip=1:Np
plot(1,ip,'o')
hold on
end
hp=findobj(gca,'type','line');
[~,ii]=sort(double(hp));
hp=hp(ii);
get(hp(1),'ydata') % Lowest valued hp is last plot
% Try again with storing
clf
hpx=zeros(Np,1);
for ip=1:Np
hpx(ip)=plot(1,ip,'o');
hold on
end
clear hpx % To avoid any confusion
hp=findobj(gca,'type','line');
[~,ii]=sort(double(hp));
hp=hp(ii);
get(hp(1),'ydata') % Lowest valued hp is first plot
So, the sequence order of double values depends on whether one stores the handles or not (note that findobj is used in each case above). Perhaps it was idealistic to assume Matlab would preserve the chronological handle value assignment that has been around for years, but this new behavior seems quite arbitrary.
Is there anyway to prevent this, e.g. in environment settings? Otherwise, it seems I will have to update all codes to store the handles.
Regards, MT
Accepted Answer
More Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!