How to automate .mat file loading

6 ビュー (過去 30 日間)
Hugo
Hugo 2022 年 2 月 3 日
編集済み: Image Analyst 2022 年 2 月 4 日
Hi,
I have a MATLAB code in which I import a .mat file and select columns. The .mat file is called A.mat. I have 5 .mat files: A.mat, B.mat, C.mat, D.mat and E.mat. Is there a way to automate the code, so it can repeat every code instruction, importing the 5 .mat files by alphabetical order?
Thank you,
  1 件のコメント
ali hassan
ali hassan 2022 年 2 月 3 日
編集済み: Image Analyst 2022 年 2 月 4 日
Yes you can simply load them at the start.
listoffiles = dir('D:\Data\**\*.mat')
for k = 1 : length(listoffiles)
CurFile = listoffiles(k)
load(fullfile(CurFile.folder, CurFile.name))
end
This gets you a list of all the files ending in .mat (change if your data has a different extension) (in subfolders of Data too) and then loads them into MATLAB. Also replace the "D:\Data" with the actual path-name of your main folder.

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

採用された回答

Stephen23
Stephen23 2022 年 2 月 3 日
編集済み: Stephen23 2022 年 2 月 3 日
You can use the approaches shown in the documentation:
For example, using DIR:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
D = load(F)
... do whatever with D
end

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by