Combine hundred of mat files into one mat file (same quantity of row and column)

5 ビュー (過去 30 日間)
Pasu Surananchanok
Pasu Surananchanok 2022 年 2 月 20 日
コメント済み: Pasu Surananchanok 2022 年 2 月 20 日
Hello Sir,
I have 123 mat files with the same quantity of column and row (example in the attachments).
The problem is I want to combine all these 123 mat files into one mat file in order to plot graph.
How can I do?
Thank you for your support.

回答 (3 件)

Image Analyst
Image Analyst 2022 年 2 月 20 日

Voss
Voss 2022 年 2 月 20 日
Here's one way:
% file_names = sprintfc('bearing1_1_%d.mat',1:123); % use this for files 1-123
file_names = sprintfc('bearing1_1_%d.mat',1:3);
% load the files into a struct array:
for ii = 1:numel(file_names)
data(ii) = load(file_names{ii});
end
disp(data)
1×3 struct array with fields: a
% vertically concatenate each struct's 'a' field into
% the 'a' field of a scalar struct 'all_data':
all_data = struct('a',vertcat(data.a))
all_data = struct with fields:
a: [98304×2 double]
% plot all the data:
plot(all_data.a(:,1),all_data.a(:,2),'.');
% save the combined data to a mat file:
save('bearing1_1_all.mat','-struct','all_data');
% check the mat file:
S = load('bearing1_1_all.mat')
S = struct with fields:
a: [98304×2 double]

Ive J
Ive J 2022 年 2 月 20 日
You can load all together:
files = ["bearing1_1_1.mat", "bearing1_1_2.mat"]; % add all files here, use dir or ls
ds = fileDatastore(files, 'ReadFcn', @(x)struct2array(load(x)));
ds = readall(ds);
ds = vertcat(ds{:});
  2 件のコメント
Turlough Hughes
Turlough Hughes 2022 年 2 月 20 日
Additionally, a handy way to write the filenames is:
files = "bearing1_1_" + (1:123) + ".mat"
Pasu Surananchanok
Pasu Surananchanok 2022 年 2 月 20 日
Thank you Bro. You save my life.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by