I have a folder in which there are many files and I want to create a matrix that holds filenames with a specific pattern. For example: The folder contains files with names starting with a subject number (e.g. 03T1A.xxx.nii, 03T1A.yyy.nii) as well as filenames with specific patterns in the middle (e.g. 03T1A.c100.nii, 03T1A.c200.nii, 03T1A.c300.nii). In this specific case I am looking to extract all the filenames with the pattern c1 and c2 in the middle (e.g. 03T1A.c100.nii and 03T1A.c200.nii but not 03T1A.c300.nii).
To this point I have used the following code to create a pattern matching variable in 'pattern' which I would like to apply to the cell array of filenames I have extracted into the variable 'all_files' via the dir call.
func_path = char(strcat(input_dir, '/', subs(files), '/Func'));
pattern = 'c[12]*.nii'
all_files = dir(func_path);
all_files = {all_files.name};
I'd like to use (read. practice) regexp and doing it with string input seems easy but I am 100% stumped as to how to do it with cell input. I started trying to do something like this:
files = all_files(cellfun(@(x)regexp(x, pattern));
But it doesn't work, obviously. Could someone help me figure out what to do here if my ultimate goal is to get a matrix output with just the relevant filenames? I've been searching MATLAB answers and other Stack Overflow posts but part of my problem is I don't understand what's happening in their code snippets. I took the above line (or at the least the beginning of it) from another post but I don't know what, for example, 'x' is (an output variable?) or what's going on in the larger command such as
fin = cellfun(@(x)regexp(x, '\.', 'split'), res, 'UniformOutput', false)
Which I found in another thread. So basically, can someone help me figure out a command that will work while explaining it to me?
regexpworks natively on cell arrays, there is no need to usecellfun. See also:Regex 101as a playground for building the expression.patterndidn't match anything.c[12]*.niiwill only match a "c" followed by only 1's and 2's before the.nii. You would needc[12].*\.niiso that you can match everything else in the string betweenc1orc2and the extension