- MAF is in the base filename
- MAF is NOT in the base filename
Help with regexp of varying filenames
2 views (last 30 days)
Show older comments
I am sorting through files, trying to extract files using naming attributes.
The most common filename looks like this one with various letters/numbers after the second MESA:
'E:/MESA/IMAGES/Exam1/8015872/010612091858/images/MESA..._8015872_20010120_145658'
But there are some expections that I need to account for like (Notice the ...MAF and MESA...MAF):
'E:/MESA/IMAGES/Exam1/8015872/010612091858/images/...MAF_8015872_20010120_145658'
'E:/MESA/IMAGES/Exam1/8015872/010612091858/images/MESA...MAF_8015872_20010120_145658'
The first path works with the code below, but the second path causes problems.
startIndex = regexp(cur_study_path,'MESA');
studyName = cur_study_path( max(startIndex): end);
Is there anyway to default to:
startIndex = regexp(cur_study_path,'MAF')
when the first path isn't present, without including path names like the third path?
3 Comments
Cris LaPierre
on 27 Feb 2021
Edited: Cris LaPierre
on 27 Feb 2021
And pleaes provide the full pathnames. It's hard to propose a solution for the exceptions when we don't know all the details.
Image Analyst
on 27 Feb 2021
Do you perhaps want to do something like this
filename = strrep(filename, 'MAF', ''); % Remove MAF from filename.
or this;
for k = 1 : numberOfFiles
thisFileName = whatever % Get filename somehow...
if contains(thisFileName, 'MAF', 'IgnoreCase', true)
continue; % Skip files with MAF in the name.
end
% Else process files with no MAF in the name.
end
Answers (1)
Cris LaPierre
on 27 Feb 2021
Perform 2 checks, one for MESA, one for MAF. They use max on the combined result.
% Case 1
cur_study_path='E:/MESA/IMAGES/Exam1/8015872/010612091858/images/MESA..._8015872_20010120_145658'
siMESA = regexp(cur_study_path,'MESA');
siMAF = regexp(cur_study_path,'MAF');
studyName = cur_study_path( max([siMESA, siMAF]): end)
% Case 2
cur_study_path='E:/MESA/IMAGES/Exam1/8015872/010612091858/images/...MAF_8015872_20010120_145658';
siMESA = regexp(cur_study_path,'MESA');
siMAF = regexp(cur_study_path,'MAF');
studyName = cur_study_path( max([siMESA, siMAF]): end)
% Case 3
cur_study_path='E:/MESA/IMAGES/Exam1/8015872/010612091858/images/MESA...MAF_8015872_20010120_145658';
siMESA = regexp(cur_study_path,'MESA');
siMAF = regexp(cur_study_path,'MAF');
studyName = cur_study_path( max([siMESA, siMAF]): end)
0 Comments
See Also
Categories
Find more on Logical 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!