MATLAB: Split 3d array into ‘i’ equal mat files

arraysmatfilesmatricesmatrixsplit

Hello All,
I have a matrix (called my_matrix) which is of size() 256 x 6 x 2000. Could you please let me know how to split this array into six separate .mat files (or some other easier way) each having 256 x 1 x 2000? I tried using for loop as below but it didn't work out.
for i = 1:6
filename[i] = 'my_data[i].mat';
save(filename[i],'my_matrix(:,i,:)';'-mat');
end
Thanks! \ksnf3000

Best Answer

See if this does what you want:
M = randi(99, 10,6, 15); % Create Matrix
C = num2cell(M, [1 3]); % Cell Array
for k1 = 1:size(C,2)
filename = sprintf('my_data%d.mat', k1);
my_matrix = squeeze(C{k1})'
save(filename, 'my_matrix')
end
NOTE I tested everything except the save call because I didn’t want to have to go back and delete those files. It should work as written.