Newer Matlab versions assign sequence of plot handle double-values in a code-dependent way

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Oct 2022
Déplacé(e) : Steven Lord le 7 Oct 2022
I am not clear on why you would do this, instead of relying on the Children order of the axes? Are you re-ordering the Children but still needing to know the original order?
The Children order is constructed as newest being the first in the list and oldest being the last in the list.
Note that if you plot more than one line in a single call, that the recorded handle order is that the first handle corresponds to the first line specified, the second to the second line specified, and so on. If you are plotting a 2D array and the number of x values matches the number of rows in the y values, then one line is generated for each column, and the first handle recorded would be for the first column, the second handle for the second column, and so on. If you are plotting a 2D array and the number of x values does not match the number of rows in the y values but does match the number of columns in the y values then the y values are silently transposed, in which case the first handle recorded would correspond to the first row of y, and so on. But in each case, then axes Children order will be Last In First Out, unless you change the order.
  1 commentaire
Mark Thomson
Mark Thomson le 6 Oct 2022
Modifié(e) : Mark Thomson le 7 Oct 2022
Dear WR,
many thanks for the fast reply. I was only sorting them in the example to illustrate the numerical sequence of double(hp).
But your advice is indeed a solution to my problem: rely on the sequence returned by get(gca,'children') or findobj(gca,'type','line') being in reverse chrononlogical order.
(My older approach worked for over a decade of versions, but apparently an aspect MW did not consider worth preserving.)
Regards,
MT

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by