How to automate .mat file loading

6 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by