converting multiple file of 4D to 3D files

1 view (last 30 days)
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
  4 Comments
Rik
Rik on 3 Jul 2020
I don't know what your variables mean, they're your variables.
The 2 is just an example, as you didn't secribe what value # should have. You wanted a length of 1 in the fourth dimension, that is what this code does. It selects the second book. (in general the first 2 dimensions of an array are called rows and columns, the third is often called page, and some people call the fourth dimension book (i.e. a collection of pages))

Sign in to comment.

Accepted Answer

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi on 5 Jul 2020
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!

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!