Searching for a file containing certain words
Show older comments
Hi,
Want to search for a file that has word1, word2 and word3 in the file name. I realize I can do this by using File=dir('* word1 * * word2 * * word3 *') However, I would like to be able to loop over different values of each word to select different files.
For example I want to find files containing the following words from many files:
A, 1, top
B, 1, top
C, 1, top
I thought I could make word1 into an array containing A, B and C but then I cannot use the dir statement above. Any suggestions would be appreciated. Many Thanks
Answers (2)
Akira Agata
on 28 Sep 2017
How about the following code?
word1 = {'A','B','C'};
for kk = 1:numel(word1)
str = ['*',word1{kk},'*1*top*'];
File=dir(str);
% Some process here
...
end
Walter Roberson
on 28 Sep 2017
0 votes
I would recommend loading in all of the file names first, and then doing appropriate string searches, using regexp() or strfind() or contains()
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!