Combining matrices from workspace or from directory into a single matrix.

Illustration
Christian - 2020-07-20T12:47:43+00:00
Question: Combining matrices from workspace or from directory into a single matrix.

Can someone explain how to combine .mat files present in workspace or in directory into one .mat file? The problem is that the files are not properly named and the files have different rows but same number of coloumns.

Expert Answer

Profile picture of Neeta Dsouza Neeta Dsouza answered . 2025-11-20

Assuming that each .mat file contains exactly one array with compatible sizes:

 

D = 'path to the directory where the files are saved';
S = dir(fullfile(D,'*.mat'));
N = numel(S);
C = cell(1,N);
for k = 1:N
    F = fullfile(D,S(k).name);
    C(k) = struct2cell(load(F));
end
M = vertcat(C{:})
save('new.mat','M')


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!