How to collate two mat files with similar name pattern from two different sub-folders?

4 ビュー (過去 30 日間)
Tomaszzz
Tomaszzz 2021 年 12 月 13 日
コメント済み: Tomaszzz 2021 年 12 月 15 日
Hi all,
I have a folder with subfolders containing multiple mat files that have a smiliar name pattern.
I would like to access each subfolder and collate two mat files that have similar name pattern. For example two files indicated red above both end in 'T001'. I would like to collate those two and save it as a separte file . Similalry, I would like to do the same for each pair of mat files that have similar name pattern ending . Number of mat files in each subfolder is the same and endings are the same. Can you help please?
I can list files and load them in a loop but not sure how to progress
clear all
clc
%% Specify each subfolder containing all mat files
subfolder1 = 'C:\Users\whctc4\Desktop\Data\EWWD_ID001\Processed data\Xsens Awinda MTw';
subfolder2 = 'C:\Users\whctc4\Desktop\Data\EWWD_ID001\Processed data\Xsens Dot';
%% get file names and start loading and collating files in a loop
all_Mt2files = dir(fullfile(subfolder1, '*.mat'));
all_Dotfiles = dir(fullfile(subfolder2, '*.mat'));
%% load files from subfolder 1
for k = 1:length(all_Mt2files)
F = fullfile(subfolder1,all_Mt2files(k).name);
load(F);
end
%% load files from subfolder 2
for k = 1:length(all_Dotfiles)
J = fullfile(subfolder2,all_Dotfiles(k).name);
load(J);
end
%% Collate loaded files according to name pattern
  2 件のコメント
Voss
Voss 2021 年 12 月 13 日
The best way to combine each pair of .mat files depends on the contents of the .mat files. For example, if they each contain only a matrix, should each pair of matrices be concatenated vertically, concatenated horizontally, concatenated in the third dimension, interleaved somehow, etc.? Are the matrices guaranteed to be compatible sizes for the way they should be combined? If the .mat files contain some other data structure, how should they be combined?
If you can upload a pair of .mat files and explain how they should be combined (maybe an example output .mat file, if available), it would be helpful.
Tomaszzz
Tomaszzz 2021 年 12 月 13 日
Hi @Benjamin . thank you for your answer. I have uploaded two example mat files. These mat files are tables with variables in each column. In the attached examples are tables 1918x115. So ultimately, the end product of collating such mat files would be a single mat file table 1918x230.

サインインしてコメントする。

採用された回答

Jan
Jan 2021 年 12 月 13 日
Maybe you want:
all_Mt2files = dir(fullfile(subfolder1, '*.mat'));
all_Dotfiles = dir(fullfile(subfolder2, '*.mat'));
for k = 1:length(all_Mt2files)
F = fullfile(subfolder1,all_Mt2files(k).name);
TF = load(F);
J = fullfile(subfolder2,all_Dotfiles(k).name);
TJ = load(J);
T = join(TF, ZJ);
% And now?
end
What exactly is a "single mat file table 1918x230"? Do you want to save T in a MAT file?
  3 件のコメント
Jan
Jan 2021 年 12 月 14 日
What do the MAT files contain? table objects? With which name?
What is the contents of TF and TJ after loading?
Maybe you need
T = join(TF.nameOfYourTable, ZJ.nameOfYourOtherTable);
save(fullfile(subfolder1, ['Joined', all_Mt2files(k).name]), 'T');
Tomaszzz
Tomaszzz 2021 年 12 月 15 日
@Jan Thank you, the above is exactly what I meant.
The mat files indded contain table objects. I did not know that I have to specify the name of the tables.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by