Borrar filtros
Borrar filtros

How to automate .mat file loading

6 visualizaciones (últimos 30 días)
Hugo
Hugo el 3 de Feb. de 2022
Editada: Image Analyst el 4 de Feb. de 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 comentario
ali hassan
ali hassan el 3 de Feb. de 2022
Editada: Image Analyst el 4 de Feb. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 3 de Feb. de 2022
Editada: Stephen23 el 3 de Feb. de 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

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by