How can I convert 3d MRI data (128x128x27) into 4d (128x128x1x27) for visualization using montage?

5 views (last 30 days)
I am going to visualize my mri data using the tutorial:
my data size 128x128x27, it is not working with the instructions. it gives the following the error:
"Unrecognized function or variable map"
I found that the data in the give tutorial size is 128x128x1x27.
How can I convert from 128x128x27 to 128x128x1x27?
Thanks

Accepted Answer

Voss
Voss on 27 Apr 2022
Edited: Voss on 27 Apr 2022
mri.mat is a .mat file included with the MATLAB installation that contains a few variables:
which mri.mat
/MATLAB/toolbox/matlab/demos/mri.mat
S = load('mri.mat')
S = struct with fields:
D: [128×128×1×27 uint8] map: [89×3 double] siz: [128 128 27]
As you can see, mri.mat contains variables D, map and siz. When you load mri.mat into your workspace then those variables become variables in your workspace:
clear
load mri
whos
Name Size Bytes Class Attributes D 128x128x1x27 442368 uint8 map 89x3 2136 double siz 1x3 24 double
The error says, "Unrecognized function or variable map". That's because, however you are loading your data (perhaps from your own .mat file or perhaps some other way), there is no variable called map in the workspace.
The error is not caused by your data being the wrong size or having the wrong dimensionality, but you can make it 128x128x1x27 by doing:
data_4D = permute(data_3D,[1 2 4 3]);
You'd still have to define a map variable if you want to use a colormap, but you can make a montage without one. And you can make a montage with 3D data:
subplot(2,2,1)
montage(D,map); % 128x128x1x27, with map
title('4D, with map');
subplot(2,2,2)
montage(D); % 128x128x1x27, no map
title('4D, no map');
subplot(2,2,3)
montage(squeeze(D),map); % 128x128x27, with map
title('3D, with map');
subplot(2,2,4)
montage(squeeze(D)); % 128x128x27, no map
title('3D, no map');

More Answers (1)

Simon Chan
Simon Chan on 27 Apr 2022
B = permute(A,[1 2 4 3])

Community Treasure Hunt

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

Start Hunting!