Newer Matlab versions assign sequence of plot handle double-values in a code-dependent way
2 views (last 30 days)
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
0 Comments
Accepted Answer
Walter Roberson
on 6 Oct 2022
Moved: Steven Lord
on 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.
More Answers (0)
See Also
Categories
Find more on Logical 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!