Clear Filters
Clear Filters

Overlaying a figure inside a for loop

6 views (last 30 days)
h h
h h on 13 Dec 2015
Answered: Image Analyst on 13 Dec 2015
I have a file of the Earth's landmass, where sea pixels are values of 0 and land pixels are values of 1. Sea pixels are blue and land pixels are green by defining
imagesc(landmass)
colormap('winter')
Now I want to plot the spread of a disease (matrix with the same size as landmass) on top of this map with a for loop which dynamically updates this spread. However, I do not know how do it correctly. When there is no disease in a certain pixel, it should just display the landmass file. When there is a disease on the pixel, it has to be overwritten by the disease matrix.
image(disease);
map = 'jet';
colormap(map);
I tried this, but it doesn't work (so this is inside the loop which updates the disease matrix):
for t=1:365
disease=disease+t;
figure(1);
imagesc(landmass)
colormap('winter')
hold on;
if disease>0
image(disease);
map = 'jet';
colormap(map);
end
title(sprintf('Day %i',t));
drawnow;
end
What is wrong? Is there a proper way to do it?
Thanks in advance!

Answers (2)

Walter Roberson
Walter Roberson on 13 Dec 2015
Part of your difficulty is that you are using different colormaps for the two images. There can only be one colormap per figure before R2014b and as of R2014b there can be one colormap per axes. At the very least you would need to create the images in different axes with R2014b or later in order to get the colormaps right. To place the images in the same axes, or with R2014a and earlier in either case, one of the images needs to be converted from pseudocolor to truecolor. My recommendation would be to draw the landmass first and then use the File Exchange contribution freezeColors() to convert it to truecolor (RGB), after which you can draw on top of it and change the colormap without affecting it.
Now that you have the landmass image()'d and colormap'd and freezeColor, hold('on') and image() the disease data on top of it, passing in the optional AlphaData parameter, with that parameter set to 1 where the data is non-zero and 0 where it is zero. Alpha of 0 means transparent.
imh = image(disease, 'AlphaData', double(disease > 0));
Then for iterations after that, you can just update the CData and AlphaData properties associated with imh with the appropriate new values instead of using image() again:
set(imh, 'CData', disease, 'AlphaData', double(disease>0));

Image Analyst
Image Analyst on 13 Dec 2015

Categories

Find more on Agriculture in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!