Ploting heatmaps in function of time

Hello,
I have search a solution for my problem but didn't find any that suited my problem.
Basically, I have a 3D matrix (18x17x2000) representing heatmap of a sample on the 2000 frames of the measurement. I would like to plot it all but with a slider or a cursor or whatever that could allow me to chose which frame I want to display, allowing me to easily go through my data.
I tried the VideoWriter tool but it gives really blurry and not convenient result. The goal is to keep the heatmap representation.
Thank you for your help,

 Accepted Answer

Adam Danz
Adam Danz on 27 Sep 2022
Edited: Adam Danz on 28 Sep 2022
This demo produces a heatmap with random data and a slider that controls which slice of the 3rd dimension of data to plot.
Demo based on a similar solution for bar plots but uses a ValueChangingFcn to update the heatmap and title as the slider changes instead of a ValueChangedFcn which is evoked after making a change to the slider.
data = rand(20,20,2000); % data with size n*m*k
fig = uifigure();
uip = uipanel(fig,'Position', [20 100 500 300]);
heatObj = heatmap(uip, data(:,:,1));
title(heatObj, 'Frame #1');
n = size(data,3);
uis = uislider(fig,'Position',[50 50 450 3],...
'Value',1, ...
'Limits', [1,n], ...
'MajorTicks', [1, 200:200:n], ...
'MinorTicks', []);
uis.ValueChangingFcn = {@sliderChangingFcn, data, heatObj};
function sliderChangingFcn(~,event,data,heatObj)
% Update heatmap and title with new selection of data
value = round(event.Value);
heatObj.ColorData = data(:,:,value);
heatObj.Title = sprintf('Frame #%d',value);
end
Tip: set clim so the colorbar limits do not jump around as the heatmap data are updated. This is seen in the GIF below.

8 Comments

It is exactly what I needed, thank you very much. Hoping this can help others with the same issue as mine.
I have another related question. Do you think it is possible to make the heatmap change as the cursors is moving and not only when the cursos is release ?
Thank you very much for your help really. I dare ask one last question. Here is the relevant code part of the plot:
data = M;
fig = uifigure();
uip = uipanel(fig,'Position', [20 100 500 300]);
hh = heatmap(uip, data(:,:,1),'Colormap', hsv,'CellLabelColor', 'none','ColorLimits', [-360 50]);
title(hh, 'Electrode Fluorescence Heatmap - Frame #1');
n = size(data,3);
uis = uislider(fig,'Position',[50 50 450 3],...
'Value',1, ...
'Limits', [1,n], ...
'MajorTicks', 0:200:n, ...
'MinorTicks', []);
%valueChangedFcn for actualisation only when cursor is dropped
uis.ValueChangingFcn = {@sliderChangingFcn, uip, data};
function sliderChangingFcn(h,~,uip,data)
value = round(h.value);
hh = heatmap(uip,data(:,:,Value), 'Colormap', hsv, 'CellLabelColor', 'none', 'ColorLimits', [-360 50]);
title(hh, sprintf('Electrode Fluorescence Heatmap - Frame #%s',num2str(value)))
end
However, when i slide the cursor it changes the plot one time during the sliding but then stays and lag/block on this last plot. I have to drop it and move it again to diplay a new plot again. I found this guy who had a similar problem but I don't know how to adapt it to my situation: https://www.mathworks.com/matlabcentral/answers/521185-how-to-continuously-change-the-plotted-data-according-to-the-value-obtained-from-a-uislider-compon
Can you help me ?
Thank you very much.
I've updated my answer to use a ValueChangingFcn. The updated answer also updates the existing heatmap rather than recreating it which is much more efficient.
Thank you it is perfect!
You may want to set clim so the colorbar limits do not jump around as you update heatmap. You can see this happening in the GIF I shared.
Where would I embed clim in the code above. I tried making it stand alone in the main script and in the function and it doesn't seem to be impacting anything.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!