Using regular expressions to filter files

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

 Réponse acceptée

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 commentaires

Thank you very much for the response. I don't think I have explained my problem well here (or I may just not understand how to apply your suggestion).
I need to create a script that iterates through a loop containing the numbers 1 to 20. So, on the first iteration, the cell array created will contain all the 'nii,1' images (which I will then perform some analysis on). On the second iteration, the array will contain the 'nii,2', and so on until ;nii,20'.
So in order to create the array, I need to find the files from the relevant directory and I cant filter simply uising *.nii (becauase that directory contains loads of nii files and some of them are irrelevant). I therefore think I need a reg expression that will match the appropriate file names (using the pattern described above) but also a variable at the end (from 1:20) that corresponds to the right frame. I can't figure out the regepression needed that matches the pattern and also includes a variable for the nii frame.
Does that make sense?
My pleasure!
Why not just something like this, then —
C = {'long/string_s1_.nii,1'
'long/string_s2_.nii,10'
'long/string_s2_.nii,1'
'long/string_s1_.nii,20'};
numbers = str2double(extractAfter(C,','))
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'}
Beyond that, I’m lost.
.
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.
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.
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by