function show_matvol(V,clim)
% SHOW_MATVOL   Shows volumetric data
%   show_matvol(V,clim)
%   Inputs: volume, grayscale color limits (optional)
%   Copyright 2018 Vedrana Andersen Dahl, vand@dtu.dk 

figure('Units','Normalized','Position',[0.1 0.3 0.5 0.6],...
    'KeyPressFcn',@key_press);
dim = size(V);
if nargin<2 || isempty(clim)
    clim = [min(double(V(:))),max(double(V(:)))];
end
z = round(0.5*(dim(3)+1));
%update_drawing
i = imagesc(V(:,:,z),clim); 
title(['slice ',num2str(z),'/',num2str(dim(3))]), axis image ij
colormap gray, drawnow

%%%%%%%%%% CALLBACK FUNCTIONS %%%%%%%%%%
    function key_press(~,object)
        % keyboard commands
        switch object.Key
            case 'uparrow'
                z = min(z+1,dim(3));
            case 'downarrow'
                z = max(z-1,1);
            case 'rightarrow'
                z = min(z+10,dim(3));
            case 'leftarrow'
                z = max(z-10,1);
            case 'pagedown'
                z = min(z+50,dim(3));
            case 'pageup'
                z = max(z-50,1);
        end
        update_drawing
    end

%%%%%%%%%% HELPING FUNCTIONS %%%%%%%%%%
    function update_drawing
        set(i,'CData',V(:,:,z))
        title(['slice ',num2str(z),'/',num2str(dim(3))])
        drawnow
    end

end

