Azzera filtri
Azzera filtri

How to automate .mat file loading

6 visualizzazioni (ultimi 30 giorni)
Hugo
Hugo il 3 Feb 2022
Modificato: Image Analyst il 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 Commento
ali hassan
ali hassan il 3 Feb 2022
Modificato: Image Analyst il 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.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 3 Feb 2022
Modificato: Stephen23 il 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

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by