MATLAB Answers

Figure colorbar is not refreshed

15 views (last 30 days)
I recently updated MATLAB from R2016b to R2019b, and noticed my codes drawing figures do not work in the same way.
When I draw an image with a colorbar and set scaled CDataMapping, the image reflects scaled color scheme but the scales on the colorbar is not refreshed.
How I can make them refreshed every time I update CData of the image?
Below is my example in which colorbar scale is not refreshed.
Thank you,
read = zeros(4, 'uint16');
readcount = 0;
fig = figure;
img = image (read,'CDataMapping','scaled');
c = colorbar;
while 1 == 1
readcount = readcount + 1;
read(mod(readcount,16)+1) = read(mod(readcount,16)+1) + readcount;
img.CData = read;


Show 1 older comment
Hiroyuki Kobayashi
Hiroyuki Kobayashi on 9 Dec 2019
Actually it IS what I wanted, thank you.
Problem was that even when caxis is set to auto (and it is auto by default), it does not re-draw colorbar labels (scales) automatically. For example, when I added caxis('auto') outside the while loop, it automatically re-adjusted color mapping of the image, but colorbar labels stay the same and did not show correct values somehow.
Now only the problem is that adding caxis inside the loop slows down the script a lot..
Adam Danz
Adam Danz on 9 Dec 2019
Oh, then problem solved! I copied my comment to the answer section and I also explained by caxis('auto') didn't do what you expected it to do.
Adam Danz
Adam Danz on 9 Dec 2019
"Now only the problem is that adding caxis inside the loop slows down the script a lot.."
Maybe if you could explain the end goal of the program I could suggest some alternatives. Currenlty the colorbar updates so quickly that it's values are nearly meaningless which was why I doubted that this was what you wanted.
For example, what's the key bit of info you want? Just the endpoints of the color range?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 9 Dec 2019
Edited: Adam Danz on 10 Dec 2019
Control the limits of the colorbar using caxis(). This line adjusts the colorbar limits to the range of your data.
The reason why caxis('auto') doesn't adjust your colorbar limits in this case is because it only responds when the colormap indexing array changes. To demonstrate this, run this line of code after a few iteration of your currently existing code.
You could also use the clim property of the axes
ax = gca();
ax.CLim = [min(read(:)),max(read(:))]


Hiroyuki Kobayashi
Hiroyuki Kobayashi on 10 Dec 2019
Thank you but still it is not clear for me. So, I show simpler example.
The following code
i = image([1 3;4 6], 'CDataMapping','scaled');
This gives result like this
Then if I update i.CData
i.CData = [15 0;-10 -5];drawnow;
It changes the figure as follows.
2019-12-10 (2).png
In this figure, colormap in the image is apparently re-adjusted to new limit [-10 ; 15], so it looks colormap indexing array is changed, but it does not trigger renewal of the numbers shown in the colorbar, and it is somehow locked to the former value [1, 6].
As you told me, if I either redraw colorbar, change color scheme or execute caxis('auto') again, it shows correct colorbar limit, but of course it does not change how image looks.
2019-12-10 (3).png
I wanted to syncronize the update of image colormap and renewal of number shown on colorbar without redefining colorbar in the figure.
I thought this is automatically syncronized in 2016b and was wondering why it stopped working in 2019b
Adam Danz
Adam Danz on 10 Dec 2019
"In this figure, colormap in the image is apparently re-adjusted to new limit [-10 ; 15],"
No, the colormap has not changed just because the CData has changed. You have set CDataMapping to 'scaled' which means the CData values are scaled to range between the minimum and maximum color limits. The CLim property of the axes contains the color limits and the clim property doesn't change just because you entered new CData. Note that in stead of setting caxis() you could set the CLim property.
ax = gca();
ax.CLim = [min(read(:)),max(read(:))]
Perhaps this background info may be helpful to you:
Hiroyuki Kobayashi
Hiroyuki Kobayashi on 10 Dec 2019
So, in the example above, after
i.CData = [15 0;-10 -5];
ax = gca()
Both shows color limit is adjusted to [-10 ; 15] after changing CData, isn't it?
As I told you, if I run the same example with R2016b, the behavior of the colorbar is quite different and its label is renewed by changing CData, contrary to what you pointed out.
Anyway, thank you for the discussion, it helped a lot to pinpoint the cause of this glitch.

Sign in to comment.

More Answers (0)

Sign in to answer this question.