Help with regexp of varying filenames

2 Ansichten (letzte 30 Tage)
Dale Black
Dale Black am 26 Feb. 2021
Beantwortet: Cris LaPierre am 27 Feb. 2021
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 Kommentare
Cris LaPierre
Cris LaPierre am 27 Feb. 2021
Bearbeitet: Cris LaPierre am 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
Image Analyst am 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 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'
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)
studyName = 'MESA..._8015872_20010120_145658'
% 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)
studyName = 'MAF_8015872_20010120_145658'
% 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)
studyName = 'MAF_8015872_20010120_145658'

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by