Updating CData of subimage unwantedly resets its colormap

Hi. Below is a class which manages 2 subimages in 1 figure. Both have different colormaps. However, as soon as i try to update the CData of either subimage, its colormap also gets reset. This is not what I want. Instead, I want the colormap to remain the same. Besides redrawing the entire figure, I'm unsure how.
classdef ImageTest
properties
image_1;
image_2;
end
methods
function obj = ImageTest()
figure(1);
subplot(1,2,1);
obj.image_1 = subimage(rand(5)*100, autumn);
subplot(1,2,2);
obj.image_2 = subimage(rand(5)*100, gray);
end
function obj = Change(obj)
set(obj.image_1, 'cdata', rand(5)*100);
drawnow;
end
end
end

 采纳的回答

Matlab uses index-data in combination with a colormap or true color data, which does not depend on a colormap. See:
That's the best description I find.
.
The first input argument of ind2rgb and ind2rgb8 is supposed to be a matrix of indices (into the colormap), i.e. a matrix of whole numbers. IMO: It's a side effect of calling a mex-function that makes ind2rgb8 work in your case. The error handling doesn't gracefully cover this. Inserting round makes ind2rgb work.
set(obj.image_1, 'cdata', ind2rgb( round(rand(5)*100), autumn ) );

更多回答(1 个)

Found a solution in changing the update code to this:
set(obj.image_1, 'cdata', ind2rgb8(rand(5)*100, autumn));
Not entirely sure if this is the best way. Also not sure why ind2rgb8 works where ind2rgb fails.

产品

Community Treasure Hunt

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

Start Hunting!

Translated by