Colormap is changed in the background without notice

1 view (last 30 days)
Hi. As I was programatically using the Colormap property, I found out a weird behavior that was totally unexpected to me.
Look at the following code:
mymap = zeros(100,3);
fig = figure; hold on; ax= fig.CurrentAxes;
el = addlistener(ax, 'Colormap', 'PreSet', @(h,e) fprintf('PreSet\n'));
el2 = addlistener(ax, 'Colormap', 'PostSet', @(h,e) fprintf('PostSet\n'));
el3 = addlistener(ax, 'Colormap', 'PreGet', @(h,e) fprintf('PreGet\n'));
fprintf('Changing colormap...\n'); colormap(mymap); fprintf('... done.\n');
printInfo(ax, '1) ');
surfl(ax, ones(3,3));
printInfo(ax, '2) ');
pause(1);
printInfo(ax, '3) ');
function printInfo(ax, str)
fprintf('%ssize(Colormap) = [%d,%d] for figure at position [%f %f %f %f])\n', str, size(ax.Colormap), ax.Position);
end
The output that I get when I save this as a script file and run it under MATLAB Version: 9.4.0.813654 (R2018a) on Linux 4.17.14-202.fc28.x86_64 is as follows:
Changing colormap...
PreSet
PostSet
... done.
PreGet
1) size(Colormap) = [100,3] for figure at position [0.130000 0.110000 0.775000 0.815000])
PreGet
2) size(Colormap) = [64,3] for figure at position [0.130000 0.110000 0.775000 0.815000])
PreGet
3) size(Colormap) = [100,3] for figure at position [0.130000 0.110000 0.775000 0.815000])
The listeners show that the colormap is changed once in the beginning, and accessed for reading three times, each time to display the information about it in the printInfo function.
However, in the second execution of that function, we see that the colormap has changed (it is indeed restored to the default colormap), and if we wait for some time, it is restored back to the customized colormap (as is confirmed by the third execution of printInfo). The overall effect is usually negligible: the colormap being restored, the figure colors are rendered properly eventually (and this is so fast that one does not usually notice it). But this destroys in practice the possibility of using the Colormap property programatically: how and and when to be sure that the Colormap property of an axis really reflects the intended colormap? Does anyone here have comments or idea on this matter?

Accepted Answer

Sylvain Chevillard
Sylvain Chevillard on 16 Jul 2019
Without answer to my question from the community, I finally decided to report it as a bug to the Matlab developers.
For the record, I got the following answer: this was indeed a bug, and it is fixed as of Matlab R2019a. For earlier versions, a workaround is to call the function drawnow after the call to surfl (or more generally, before any attempt to programmatically access a graphic property).

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!