how to match with random cell name with varied format

1 view (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 23 Dec 2014
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?
  2 Comments
Jiali
Jiali on 23 Dec 2014
Thank you for your time. You give clear explanation for what I describe. I need more help. In my case, all these are name of files. I search to match file name with input array. The input array will be matched with XXXX (e.g. cellnumb1025). Then I need to further find whether XXXX_AM or XXXX_PM to get newest data for same filename. ( I have lots of data, and each sample may be tested more than more times. For same period of time, I already sorted from small to large according to time, but it doesn't work for XXXX_PM or XXXX_AM) Currently, my thought will be compare file name with input array +whatever in middle + AM/PM. But I have trouble to implement it.
Image Analyst
Image Analyst on 23 Dec 2014
I didn't quite follow. But can't you just use strfind() like I did to search for whatever you want?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!