Unexpected behaviour of sliceViewer 'SliceDirection'
Show older comments
I'm not really sure how to reconcile the 'SliceDirection' in sliceViewer. Matlab's documentation says SliceDirection = [0 1 0] slices along the second dimension. But this is clearly not the case. In the MWE SliceViewer shows a line in the middle slice, and black everywhere else. I would expect each slice to have a bright spot in the middle. The imshow() outputs the expected result, a single bright pixel.

matlab sliceViewer documentation says:
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"
MWE:
clear all; clc;
N = 40;
full_vol = zeros(N, N, N, 'uint8');
full_vol(N/2, :, N/2) = 255;
figure(); sliceViewer(full_vol, 'SliceDirection', [0 1 0], 'SliceNumber', N/2);
figure(); imshow(squeeze(full_vol(:, N/2, :)))
Answers (1)
Kautuk Raj
on 3 Jul 2025
0 votes
This is arising from how MATLAB indexes arrays and how sliceViewer interprets the 'SliceDirection' parameter. In MATLAB, a 3D array is indexed as (row, column, page), which corresponds to (Y, X, Z).
When you use 'SliceDirection', [0 1 0], you are moving through the first dimension (Y, or rows), so each slice is an X-Z plane at a fixed Y. In your example, only the slice at Y = N/2 (i.e., SliceNumber = N/2) shows the line you set; all other slices are black.
So, your code is behaving as expected according to MATLAB's conventions.
Categories
Find more on Morphological Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!