Using regular expressions to filter files
18 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Star Strider
2022-10-28
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,','))
A regexp call could be constructed for this, however extractAfter is easier, if available to you.
.
4 个评论
Star Strider
2022-10-28
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')
numbers = str2double([out{:}]).'
Lv = numbers == 1 % Logical Vector
filename = C(Lv) % Retrieve File Names
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!