Using regular expressions to filter files
20 ビュー (過去 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!