Browse image slices


The sliceViewer object opens a viewer for exploring grayscale and RGB volumes.

Use sliceViewer to look at individual slices in the volumes. When the viewer opens, it displays the middle image in the stack in the direction specified by the SliceDirection property. Use the slider to navigate through the volume and view individual slices.

The sliceViewer object supports properties, object functions, and events that you can use to customize its appearance and behavior. sliceViewer can send notifications when certain events, such as the slider moving, occurs. For more information, see Events.

By default, you can interactively adjust the brightness and contrast of the slice displayed in the slice viewer by dragging the mouse horizontally or vertically over the image. For more information, see Interactive Behavior.




sliceViewer(S) displays the grayscale or RGB volume S in a figure. The figure includes a slider that you can use to view individual slices of the volume.

sliceViewer(S,Name,Value) sets properties using name-value pairs. For example, sliceViewer(S,'Colormap',cmap) creates a sliceViewer object specifying the colormap used to display the volume. You can specify multiple name-value pairs. Enclose each property name in single quotes.

sv = sliceViewer(___) returns a sliceViewer object, sv, with properties that can be used to control visualization of the volume. Use input arguments from any of the previous syntaxes.

Input Arguments

expand all

Input volume, specified as an m-by-n-by-p-by-c numeric array. For grayscale volumes, c is 1. For RGB volumes, c is 3. RGB volumes can be only of class uint8, uint16, single, or double.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32


expand all

Colormap of the image stack, specified as an m-by-3 numeric array with values in the range [0 1]. When S is an RGB image stack, Colormap has no effect.

Display range of image stack, specified as a two-element numeric vector of the form [low high]. The value low (and any value less than low) displays as black. The value high (and any value greater than high) displays as white. Values in the range [low high] display as intermediate shades of gray, using the default number of gray levels. If you specify an empty matrix ([]), the sliceViewer object uses the default value. When you specify a stack of RGB images, DisplayRange has no effect.

Enable interactive control of display range, specified as one of the values in this table. To learn more about interactive behavior, see the Creation section.

'on' (default)Control the display range of a grayscale image stack by left-clicking the mouse and dragging it on the axes.
'off'Disable display range interactivity.

Parent of the sliceViewer object, specified as a handle to a uipanel or a figure created with the figure or uifigure commands. If you do not specify a parent, the parent of the sliceViewer object is gcf.

Scale factors used to rescale the volume, specified as a 1-by-3 positive numeric vector. The values in the array correspond to the scale factor applied in the x, y, and z directions.

Direction in which to browse image stack, specified as 1-by-3 logical vector or one of the character vectors in this table.

Character VectorLogical VectorDescription
'X' [1 0 0]Browse in X direction
'Y'[0 1 0]Browse in Y direction
'Z' (default)[0 0 1]Browse in Z direction

Index of the slice to be displayed from the volume, specified as a positive numeric scalar.

Object Functions

addlistenerCreate event listener bound to event source
getAxesHandleGet handle to axes in slice viewer


collapse all

Load MRI data into the workspace.

load mristack

View slices in slice viewer with custom Colormap. the Slice Viewer opens the stack of images, displaying the middle one. You can use the slider to select which slice to view.

cmap = parula(256);
s = sliceViewer(mristack,'Colormap',cmap);

Load MRI data into the workspace.

load mristack

View the data in the Slice Viewer.

s = sliceViewer(mristack);

Get the handle of the axes containing the slice.

hAx = getAxesHandle(s);

Specify the name of the GIF file you want to create.

filename = 'animatedSlice.gif';

Create an array of slice numbers.

sliceNums = 1:21;

Loop through and create an image at slice position.

for idx = sliceNums
    % Update slice number.
    s.SliceNumber = idx;
    % Use getframe to capture image.
    I = getframe(hAx);
    [indI,cm] = rgb2ind(I.cdata,256);
    % Write frame to the GIF file.
    if idx == 1
        imwrite(indI,cm,filename,'gif','Loopcount',inf,'DelayTime', 0.05);
        imwrite(indI,cm,filename,'gif','WriteMode','append','DelayTime', 0.05);

Load a stack of images.

load mristack

Specify a custom colormap for viewing the slices.

cmap = parula(256);

View the image stack in the Slice Viewer.

s = sliceViewer(mristack,'Colormap',cmap);

Set up listeners for the two Slice Viewer slider moving events. When you move the slider, the Slice Viewer sends notifications of these events and executes the callback function you specify.


The allevents callback function just displays the name of each event and the current position of the slider.

function allevents(src,evt)
    evname = evt.EventName;
            disp(['Slider value changing event: ' mat2str(evt.CurrentValue)]);
            disp(['Slider value changed event: ' mat2str(evt.CurrentValue)]);

More About

expand all

Introduced in R2019b