Using regular expressions to filter files

18 views (last 30 days)
Hi everyone,
I need to use regular expressions to filter through a directory of nii files and create a cell array of files that match the requirements. However, I am new to matlab and there are just too many requirements for me to work out the regular expression needed.
The desired cell array looks like this (but note that the nii files have 20 frames so I want the script to loop through 20 times):
In the past, I have been able to create these arrays by just using dir and filtering using ‘*.nii, however, the directory I am working with has loads of different nii, files so using it here would not be specific enough.
As you can see from the picture above, the pattern I am looking for is ‘ica_sub’ + 3 digits + _component_ica_s + 1 digit + ‘_.nii, + a final digit.
As explained above, the nii files have 20 frames so the 'final digit' in the expression will need to be a variable containing the numbers 1 to 20 (which I will loop through).
I am just really confused about how to combine all of this together. If anyone can help me out I would appreciate it so much.
Gerard

Accepted Answer

Star Strider
Star Strider on 28 Oct 2022
There appears to be only one comma (,) in each line, so perhaps the extractAfter function (introduced in R2016b) will work.
C = {'long/string_s2_.nii,1'
'long/string_s1_.nii,20'};
numbers = str2double(extractAfter(C,','))
numbers = 2×1
1 20
A regexp call could be constructed for this, however extractAfter is easier, if available to you.
.
  4 Comments
Gerard Campbell
Gerard Campbell on 28 Oct 2022
Brilliant, thanks for your help.
Even if not suitable here, out of interest, do you know what a regex pattern for the below would look like?
‘ica_sub’ + 3 digits + '_component_ica_s' + 1 digit + ‘_.nii,' + variable (i.e., 1 to 20).
I really want to learn how to use regex but struggle with it at the moment.
Star Strider
Star Strider on 28 Oct 2022
My pleasure!
There are others here who are much better at regexp calls than I am, so I will defer to them for any necessary details.
One item to consider is to use an anchor, particularly:
expr$ End of the input text. '\w*m$' matches words ending with m at the end of the text.
so perhaps:
'\d+$'
would work.
Testing it —
C = {'long/string_s1_.nii,1'
'long/string_s2_.nii,10'
'long/string_s2_.nii,1'
'long/string_s1_.nii,20'};
out = regexp(C, '\d+$', 'match')
out = 4×1 cell array
{1×1 cell} {1×1 cell} {1×1 cell} {1×1 cell}
numbers = str2double([out{:}]).'
numbers = 4×1
1 10 1 20
Lv = numbers == 1 % Logical Vector
Lv = 4×1 logical array
1 0 1 0
filename = C(Lv) % Retrieve File Names
filename = 2×1 cell array
{'long/string_s1_.nii,1'} {'long/string_s2_.nii,1'}
See the documentation under expression for details.
.

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!