How to simplify code which reads multiple files

1 回表示 (過去 30 日間)
Nom
Nom 2020 年 1 月 10 日
コメント済み: Nom 2020 年 1 月 10 日
Hello,
I have a script which runs through multiple csv files based on a query:
filePattern = fullfile(myFolder, '*ABC*.csv');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
%Rest of my code goes here
end
However, I also want it to run through files labelled 'DEF' and also run the same chunk of code that comes after the following line.
fullFileName = fullfile(myFolder, baseFileName);
I wasn' t sure how to simply this so I ended up just copying and pasting the entire code while just changing the first line to another query:
i.e.
filePattern = fullfile(myFolder, '*DEF*.csv');
This definitely isn't efficient by any means so I was hoping to get some insight in any way possible to reduce the clutter.

採用された回答

Stephen23
Stephen23 2020 年 1 月 10 日
編集済み: Stephen23 2020 年 1 月 10 日
You don't need to repeat the entire code, just the dir call is enough:
theFiles = [...
dir(fullfile(myFolder, '*ABC*.csv'));...
dir(fullfile(myFolder, '*DEF*.csv'))];
  1 件のコメント
Nom
Nom 2020 年 1 月 10 日
Thank you for your answer, this worked!
I switched over to this method because of its simplicity.

サインインしてコメントする。

その他の回答 (1 件)

Adam Danz
Adam Danz 2020 年 1 月 10 日
You can initially list all files with a given extension and then eliminate any files that do not contain one of the strings listed. This solution uses contains(), released in r2016b.
% Get all files with given extension
filePattern = fullfile(myFolder, '*.csv');
theFiles = dir(filePattern);
% Isolate files with a matching pattern
filenameOptions = {'ABC','DEF'};
matchIdx = contains({theFiles.name},filenameOptions);
theFiles(~matchIdx) = []; % Eliminate all non-matched files

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by