Figure colorbar is not refreshed

57 visualizaciones (últimos 30 días)
Hiroyuki Kobayashi
Hiroyuki Kobayashi el 9 de Dic. de 2019
Comentada: Hiroyuki Kobayashi el 10 de Dic. de 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 comentarios
Adam Danz
Adam Danz el 9 de Dic. de 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 el 9 de Dic. de 2019
Editada: Adam Danz el 9 de Dic. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 9 de Dic. de 2019
Editada: Adam Danz el 10 de Dic. de 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 comentarios
Adam Danz
Adam Danz el 10 de Dic. de 2019
Editada: Adam Danz el 10 de Dic. de 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 el 10 de Dic. de 2019
So, in the example above, after
i.CData = [15 0;-10 -5];
try
caxis
or
ax = gca()
ax.CLim
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Colormaps en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by