How to pick specific file name in directory

13 views (last 30 days)
Greeting!
I Have a problem about getting bunch of input picture with specific name. My input consist of
crop_001_S_01
crop_001_S_02
crop_001_S_03
.....
crop_002_S_01
crop_002_S_02
crop_003_S_03
.....
Where (001) represent the single individu and (S_01) representing 1 picture taken from single person. There is 10 picture for single person set ( crop_001_S_01 - crop_001_S_10). There is more than 800++ picture in single folder.
What I want to do is to take only specific number of picture from single person as train image like I only take second until forth picture from every person
crop_002_S_02 - crop_002_S_04
crop_003_S_02 - crop_003_S_04
crop_004_S_02 - crop_004_S_04
crop_005_S_02 - crop_005_S_04
......
I try to write like below
in_name = 'S_0';
for fig = 2:3
testdir = dir(['C:\FYP\work\FYP\rawpic\*', in_name(fig),'*.jpg']);
end
Yet, I still could not get the specific file required. It's there a way to pick a specific input picture name from dir? Thanks!
  4 Comments
Muhammad Bariq Azmi
Muhammad Bariq Azmi on 6 May 2018
So, I need to screen the name using name directory? Like file.name{i}?
dpb
dpb on 6 May 2018
Not exactly what we're suggesting, no...as Stephen wrote, first return the DIR() output for the group of interest or all the files and just only worry about screening from the full pool--that's the struct S in above code snippet.
Then cast the names field of the structure into a cell array; now you have a searchable cellstr array of all the names from which to do the selection matching however you wish; a regexp expression can undoubtedly be written automagically to meet whatever subset it is you wish out of those.

Sign in to comment.

Accepted Answer

Akira Agata
Akira Agata on 7 May 2018
Edited: dpb on 7 May 2018
As Stephen-san and dpb-san mentioned, regular expression will help extract files based on file name. Here is one example to extract the second until fo[u]rth picture from every person.
fileList = struct2table(dir('*.jpg'));
match = regexp(fileList.name,'crop_[0-9]{3}_S_(02|03|04).jpg');
idx = cellfun(@isempty,match);
fileList.isTrain = ~idx;
  2 Comments
Muhammad Bariq Azmi
Muhammad Bariq Azmi on 7 May 2018
Thank you, I already got the file and filter to only image input that I want. Now I can proceed with these input. Thank you again!
dpb
dpb on 7 May 2018
And may the forth be with you... :)

Sign in to comment.

More Answers (0)

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!