Finding files in directory with two different strings in filename

18 views (last 30 days)
I have a directory which is a folder (called 'DBDIR' below) and I want to find all of the files in this directory that contain two separate strings ('case#' and 'PPG') in the filename. With the below code, I can locate all files in the folder 'CNAP PPG Database' that contain the string 'case#' in the filename.
DBDIR = '/Users/me/CNAP PPG Database';
ggg = dir([DBDIR,'/case#*.mat']);
I am trying to do this to find files that have both 'case#' and 'PPG' in the filename, so I want to do something like below (this of course didn't work though).
DBDIR = '/Users/me/CNAP PPG Database';
ggg = dir([DBDIR,'/case#**PPG.mat']);
To clarify, my list of files in the directory looks like:

Accepted Answer

Stephen23
Stephen23 on 7 Jul 2023
Edited: Stephen23 on 7 Jul 2023
Use FULLFILE rather than concatenating text together.
Here is a simple approach that solves the problem you pose in your question:
DBDIR = '/Users/me/CNAP PPG Database';
ggg = [...
dir(fullfile(DBDIR,'*case#*PPG*.mat'));...
dir(fullfile(DBDIR,'*PPG*case#*.mat'))]
Here is a simple approach that works with the filenames that you actually have:
DBDIR = '/Users/me/CNAP PPG Database';
ggg = dir(fullfile(DBDIR,'case#*PPG*.mat'))

More Answers (1)

Image Analyst
Image Analyst on 7 Jul 2023

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!