1

I am dealing with a 3D double which I want to visualize slice by slice. To do that, I tried plotting a heatmap and then adding a slider to select the index for the third dimension. However, I get the following error: Error using uislider (line XX) HeatmapChart cannot be a parent.

A minimal example to reproduce my problem:

% Generate dummy 3d array
img = ones(5,4,3);
for ii=1:size(img,3)
    img(:,:,ii)=ii;
end
% Try plotting heatmap with slider
h = heatmap(img(:,:,1));
uislider(h)

Is there actually a way/workaround to use a slider on a heatmap? Thanks!

nespereira
  • 67
  • 6
  • the `heatmap` is not a valid _container_ for the `uislider`. You have to attach the `uislider` to the parent `figure` or `panel`, then assign a callback function to the `ValueChangedFcn` property of the `uislider`. In this callback function, you can update the heatmap depending on the slider value. – Hoki Feb 02 '21 at 14:38

1 Answers1

2

There is. But I think you cannot use the uislider for this. Try:

h = heatmap(img(:,:,1));
uicontrol('Style','slider');

This slider has similar functionality, but doesn't look so nice. Still you can define Min, Max, Color, ... and also implement Callback function.

In the end it could look like:

h = heatmap(img(:,:,1));
uicontrol('Style','slider','Value',1,'Min',1,'Max',size(img,3),...
          'SliderStep',[0.5 0.5],'CallBack',{@SlideThroughSlices,img})

function SlideThroughSlices(slider,~,img)

   heatmap(img(:,:,slider.Value));

end

Assuming that the size of the third dimension of img is 3. Otherwise you have to adjust Min, Max and SliderStep.

Till
  • 183
  • 7
  • Really cool! Had to check [this answer](https://stackoverflow.com/a/10452219/8552041) to be able to define the correct SliderStep, but otherwise it worked perfectly! First time using uicontrol :) – nespereira Feb 02 '21 at 16:03
  • 3
    I would change the name of the callback function: `slice` is already a buit-in MATLAB function which you might not want to overload given you are already working with _slices_. – Hoki Feb 02 '21 at 16:31
  • @Hoki, good idea, didn't know that it was a bult-in function, thanks – Till Feb 02 '21 at 19:59
  • @nespereira Anytime :) Also on the MATLAB page you find good information about uicontrol (https://de.mathworks.com/help/matlab/ref/uicontrol.html). If you try to display your current value of the slider and struggle with that, just let me know and I help you. – Till Feb 02 '21 at 20:02