필터 지우기
필터 지우기

loading multiple mat files from a directory one by one, and running a script for them

조회 수: 79 (최근 30일)
Hi,
I need to execute the following steps in matlab:
  1. Load a file (from a directory containing multiple files of interest). The order wouldn't matter. I just need to do it for all files, but one by one.
  2. Run a predefined script on that mat file.
  3. Save a variable
  4. Delete all variables and load the next mat file with its new variables
  5. Run the same process for the next mat file in the directory

답변 (2개)

KSSV
KSSV 2019년 5월 30일
matfiles = dir('*.mat') ;
N = length(matfiles) ;
iwant = cell(N,1) ; % to save output
for i = 1:N
load(matfiles(i).name)
% do what you want, let out put be out
iwant{i} = out
end

Image Analyst
Image Analyst 2022년 4월 10일
편집: Image Analyst 2022년 4월 10일
See the FAQ:
There are code samples in the FAQ to do it two different ways. In short,
matFiles = dir('*.mat') ;
numFiles = length(matFiles) ;
for k = 1 : numFiles
% Get file name of one mat file.
thisFileName = fullfile(pwd, matFiles(k).name);
fprintf('Processing "%s".\n', thisFileName);
% Load mat file variables. Any prior ones will be overwritten.
s = load(thisFileName) % This is a structure with all the variables on it as fields.
% Now "Run a predefined script on that mat file."
output(k) = PredefinedFunction(s);
end

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by