Figure colorbar is not refreshed
60 views (last 30 days)
Show older comments
Hiroyuki Kobayashi
on 9 Dec 2019
Commented: Hiroyuki Kobayashi
on 10 Dec 2019
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;
drawnow;
end
4 Comments
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
on 9 Dec 2019
Edited: 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?
Accepted Answer
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.
caxis([min(read(:)),max(read(:))]);
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.
colormap(parula(10))
[Addendum]
You could also use the clim property of the axes
ax = gca();
ax.CLim = [min(read(:)),max(read(:))]
3 Comments
Adam Danz
on 10 Dec 2019
Edited: 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:
More Answers (0)
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!