Filter löschen
Filter löschen

How to automate .mat file loading

6 Ansichten (letzte 30 Tage)
Hugo
Hugo am 3 Feb. 2022
Bearbeitet: Image Analyst am 4 Feb. 2022
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 Kommentar
ali hassan
ali hassan am 3 Feb. 2022
Bearbeitet: Image Analyst am 4 Feb. 2022
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 3 Feb. 2022
Bearbeitet: Stephen23 am 3 Feb. 2022
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

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by