imagesc output: how to re-update the matrix image only, but keep other things intact, including colorbar, axis tick, axis label, annotation, text...?

4 次查看(过去 30 天)
Hi, I want to update the imagesc generated figure window, just update the pixel matrix values of same dimension size, but keep other things intact, including cplormap, colorbar, axis tick, axis label, annotation, text,...
But the code below reset and remove everything:
f = figure;
imagesc([1,2,3;4,5,6;]);colorbar;
text(1,2,'apple')
set(gca(f),'YTick', [1,2],'YTickLabel',{'AAA','BBB'},'TickLabelInterpreter','none');
% update the matrxi (of same size) only, but leave the tick labels,text, annotations,...all intact
figure(f)
imagesc([4,5,3;4,7,6;]); % this removes all the tick labels,text, annotations,..
Is there a way to solve this problem? I am using R2016a.
Many thanks for the suggestion.

采纳的回答

Stephen23
Stephen23 2025-3-15
编辑:Stephen23 2025-3-15
"how to re-update the matrix image only, but keep other things intact, including colorbar, axis tick, axis label, annotation, text...?"
IMAGESC returns an image object. So you can simply update the image object's image CData:
imh = imagesc([1,2,3;4,5,6]);
drawnow % for some reason this does not work on the forum
imh.CData = [4,5,3;4,7,6;];
  1 个评论
Stephen23
Stephen23 2025-3-15
Here is a long and convoluted demonstration that works around limitations on this forum:
% Create a tiled layout
tiledlayout(1,2);
% First tile - original image
ax1 = nexttile;
imh = imagesc([1,2,3;4,5,6]);
title('Original Image');
colorbar;
axis image;
% Second tile - copy of the original image
ax2 = nexttile;
copyobj(get(ax1, 'Children'), ax2);
title('Copy of Original');
colorbar;
axis image;
% Now update the CData of the original image - demonstrating the syntax
imh.CData = [4,5,3;4,7,6];
title(ax1, 'After imh.CData update');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colorbar 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by