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:
- 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.
- Run a predefined script on that mat file.
- Save a variable
- Delete all variables and load the next mat file with its new variables
- Run the same process for the next mat file in the directory
0 个评论
回答(2 个)
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File 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!