How to collate mutiple mat files based on their name?

2 views (last 30 days)
Hi all,
I have multiple folders with mutiple mat files which are structures with a table inside. Please see screenshot below of an exmaple folder and example mat file attached.
I want to:
Collate files ending with the same number (as in green below, these numbers may differ depending on a folder) into one mat file with a name ending with this speciif number (for example: Dot_DatSynced_ID001_001). This mat file should be a structure with tables named LShank, LThigh, Pelvis, RShank, RThigh.
Could you please help?

Answers (2)

ag
ag on 12 Mar 2025
Hi Tomaszzz,
To collate files ending with the same number into one mat file, you can follow the below steps:
  • Use MATLAB's dir function to list all .mat files in the folder.
  • Extract unique numbers from filenames to know which files to group together.
  • Load and Combine Data: For each unique number, load the corresponding files and combine them into a structure.
  • Save Combined Data: Save the combined structure into a new ".mat" file.
The below pseudocode demonstrates how to achieve this:
% Get list of files
files = dir('*.mat');
% Extract unique endings
endings = [];
for i = 1:length(files)
% Extract number ending from filename
nameParts = split(files(i).name, '_');
ending = nameParts{end-1};
endings = [endings; ending];
end
uniqueEndings = unique(endings);
% Process each unique ending
for i = 1:length(uniqueEndings)
ending = uniqueEndings{i};
% Initialize structure to hold tables
dataStruct = struct();
% Load files with the same ending
for j = 1:length(files)
if contains(files(j).name, ending)
% Load the mat file
loadedData = load(files(j).name);
% Determine which table it is (LShank, LThigh, etc.)
if contains(files(j).name, 'LShank')
dataStruct.LShank = loadedData.table;
else ...
end
end
end
% Step 5: Save the combined structure
saveFileName = ['Dot_DataSynced_ID001_' ending '.mat'];
save(saveFileName, '-struct', 'dataStruct');
end
For more details, please refer to the following MathWorks documentations:
Hope this helps!

Voss
Voss on 12 Mar 2025
% first, make some copies of that single mat file,
% with different names, for demonstration:
F = dir('*.mat');
copyfile(F(1).name,'Dot_DataSynced_ID001_001_RShank_mat.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_001_Pelvis_mat.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_007_LShank_mat.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_007_RShank_math.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_007_RThigh_math.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_011_Pelvis_booey.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_011_baba_booey.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_011_LThigh_krokus.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_015_LLCOOLJ_quivers.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_015_RCRUMB_norris.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_015_richard_christy.mat')
copyfile(F(1).name,'Dot_DataSynced_ID001_015_sal_governale.mat')
% now perform the operations specified in the question:
out_folder = 'Collated';
F = dir('*.mat');
C = regexp({F.name},'(.*)_([^_]*)_([^_]*)_.*','tokens','once');
C = vertcat(C{:});
[G,GID] = findgroups(C(:,2));
if ~isfolder(out_folder)
mkdir(out_folder)
end
for ii = 1:numel(GID)
out = struct();
idx = find(G == ii);
for jj = 1:numel(idx)
S = load(fullfile(F(idx(jj)).folder,F(idx(jj)).name));
tmp = fieldnames(S);
if isempty(tmp)
continue
end
out.(C{idx(jj),3}) = S.(tmp{1});
end
save(fullfile(out_folder,sprintf('%s_%s.mat',C{idx(1),1},GID{ii})),'-struct','out')
end
% now check the resulting mat files
F = dir(fullfile(out_folder,'*.mat'));
for ii = 1:numel(F)
disp([fullfile('.',out_folder,F(ii).name) ':'])
S = load(fullfile(F(ii).folder,F(ii).name))
end
./Collated/Dot_DataSynced_ID001_001.mat:
S = struct with fields:
LShank: [1918x11 table] Pelvis: [1918x11 table] RShank: [1918x11 table]
./Collated/Dot_DataSynced_ID001_007.mat:
S = struct with fields:
LShank: [1918x11 table] RShank: [1918x11 table] RThigh: [1918x11 table]
./Collated/Dot_DataSynced_ID001_011.mat:
S = struct with fields:
LThigh: [1918x11 table] Pelvis: [1918x11 table] baba: [1918x11 table]
./Collated/Dot_DataSynced_ID001_015.mat:
S = struct with fields:
LLCOOLJ: [1918x11 table] RCRUMB: [1918x11 table] richard: [1918x11 table] sal: [1918x11 table]

Community Treasure Hunt

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

Start Hunting!