MATLAB: Converting multiple file of 4D to 3D files

4d to 3d

i have 80 files of 4D double (140x122x180x#)
i need the files in 3D (140x122x180)
i have tried these solutions but does not come out as expected
files = B % 140x122x180x#
threeD = permute(files[1,2,3,4])
% the result will be 4D double
% i also tried
threeD = squeeze(num2cell(permute(files,[1,2,3,4]),1:3))
% the result will be #x1 cells
current coding :
cd ('path');
files=dir('*.mat'); % find all |.mat| files in the current directory.
totalfile = numel(files); % count those files.
C = cell(1,totalfile); % preallocate a cell array for the loaded data.
for a = 1:totalfile % loop over the number of files.
T = load(files(a).name) % load the data from each file
C(a) = struct2cell(T); % convert structure to cell, allocate to cell array.
M = cat(4,C{:}) % concatenate all MxN matrices into MxNxP array,
% 3 dimensional concatenate
% ???
save(['path/zscore_' num2str(a) '.mat'],'M');
end
thank you

Best Answer

Hey Hanisah,
As mentioned by @Rik, iterate through the collection of 4D matrices to get your 3D values.
For example,
for i = 1:size(B, 4)
A = B(:,:,:,i) % Simple example to slice your 4D matrices
end
Hope this helps!