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

3 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Stephen23
Stephen23 le 15 Mar 2025
Modifié(e) : Stephen23 le 15 Mar 2025
"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 commentaire
Stephen23
Stephen23 le 15 Mar 2025
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');

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by