MATLAB: How to match with random cell name with varied format

match with random cell name

I have a series of cell array
XXXX-20-0266-3.dat
XXXX-10-01-12_03.03.03 AM.dat
XXXX-10-01-12_01.03.03 PM.dat
XXXX-30-0333.dat
I want to match with AM or PM, I try to use regexp function, regexp(cell{2}, [xxxx '-?\w*AM.csv'], it does not work. how I can achieve it since the middle always change its length and content? Please help me. I appreciate for your time and efforts.

Best Answer

Not exactly sure what kind of variable you want to get, but study this snippet:
ca = {'XXXX-20-0266-3.dat';
'XXXX-10-01-12_03.03.03 AM.dat';
'XXXX-10-01-12_01.03.03 PM.dat';
'XXXX-30-0333.dat'}
amIndexes = strfind(ca, 'AM') % amIndexes is a cell
pmIndexes = strfind(ca, 'PM') % pmIndexes is a cell
logicalIndexesAM = ~cellfun(@isempty, amIndexes)
logicalIndexesPM = ~cellfun(@isempty, pmIndexes)
linearIndexesAM = find(logicalIndexesAM)
linearIndexesPM = find(logicalIndexesPM)
In the command window:
ca =
'XXXX-20-0266-3.dat'
'XXXX-10-01-12_03.03.03 AM.dat'
'XXXX-10-01-12_01.03.03 PM.dat'
'XXXX-30-0333.dat'
amIndexes =
[]
[24]
[]
[]
pmIndexes =
[]
[]
[24]
[]
logicalIndexesAM =
0
1
0
0
logicalIndexesPM =
0
0
1
0
linearIndexesAM =
2
linearIndexesPM =
3
Does that help at all? Can you get whatever you need from that? Or do you need more help?