Unexpected behaviour of sliceViewer 'SliceDirection'

2 views (last 30 days)
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, :)))
  1 Comment
Jacques
Jacques on 29 Jun 2025
Please correct me if there is a mistake here, but I'm thinking the problem was:
It was a misunderstanding of the convention (using [x y z] for 'SliceDirection' makes it confusing).
Using 'SliceDirection' [0 1 0] will slice along the first dimension, because matlab defines the first dimension as y, the second dimension as x and the third as z.

Sign in to comment.

Answers (1)

Kautuk Raj
Kautuk Raj on 3 Jul 2025
Hello @Jacques,
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.

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!