steadyfish
steadyfish

Reputation: 877

MATLAB: Dynamic heatmap

I have 3D matrix of dimensions D x D x N. I want to create a dynamic heatmap to show how it's varying over N. Here's the MATLAB code I used to achieve this.

for n=1:N
    heatmap(dynamicCov(:,:,n));
    pause(0.5);
end

The issue with this code is that for each n, it opens a new figure window. I want it to be updated in the same Heatmap window. Is it possible to do that? Is there any other way to achieve this?

Thanks.

Upvotes: 2

Views: 1827

Answers (2)

steadyfish
steadyfish

Reputation: 877

I found a better and a lot simpler way of doing this. It uses built in imagesc() function instead of HeatMap() function from Bioinformatics toolbox. The code is as follows:

dynamicCov = rand(20,20,10); % create test data
N = size(dynamicCov,3);


for n=1:N
    imagesc(dynamicCov(:,:,n));
    colormap('copper');
    colorbar;
    pause(0.5);
end

Reference: http://buli.waw.pl/matlab-heatmap-colormap/

Upvotes: 1

Phil Goddard
Phil Goddard

Reputation: 10782

You need to use the undocumented 2nd input to HeatMap that indicates whether a plot should be created or not, and a few other Handle Graphics tricks to get the handle to the figure that is created. Something like

data = rand(20,20,10); % create test data
hmo = HeatMap(data(:,:,1),false); % create but do not plot
plot(hmo); % do the plot
allHFig = findall(0,'Type','figure'); % get handle to all open figures
hFig = allHFig(1); % we want the most recently created figure
for idx = 2:size(data,3)
   hmo = HeatMap(data(:,:,idx),false); % create heatmap but do not plot
   plot(hmo,hFig); % plot to our existing figure
   pause(0.5);
end

Upvotes: 2

Related Questions