Pattern matching problem

Hello Community,
I have some video files named using the following pattern:
SHC_LL_S1_0008_V5.5983338494.avi
RHA_RL_S3_0008_v1.59806163.avi
SLR_LL_S1_0008_v1.59806163.avi
I also have some .mat data that corresponds to each video file:
2009-10-15 09-23-54_SHC_RL_S1_0008FinalData
2009-10-15 09-59-31_SLR_RL_S3_0008FinalData
2009-10-15 09-57-11_SLR_RL_S1_0008FinalData
I need to figure out how to load the data files automatically depending on the video selected? I have some experience using the strfind(itemselected,pattern1) function but so far I havent been able to come up with the logic to slove this issue.
Maybe I could store the name of the currently playing video to a variable then try to match that? The key identifiers in the names of both files are:
SHC_LL_S1 SHC_RL_S1

 Accepted Answer

Something like this. BTW, I have to modify your video file name to find a match.
VideoFiles={'SHC_LL_S1_0008_V5.5983338494.avi';
'RHA_RL_S3_0008_v1.59806163.avi';
'SLR_LL_S1_0008_v1.59806163.avi'};
DataFiles={'2009-10-15 09-23-54_SHC_LL_S1_0008FinalData';
'2009-10-15 09-59-31_SLR_RL_S3_0008FinalData';
'2009-10-15 09-57-11_SLR_LL_S1_0008FinalData'};
for k=1:length(VideoFiles)
Pattern=VideoFiles{k}(1:9);
Index=strfind(DataFiles,Pattern);
Index=cellfun(@isempty,Index);
FoundDataFile=DataFiles(~Index)
end

11 Comments

I see. I'm still getting the hang of looping in matlab. Can you explain some of these lines so I can get the hang of this?
for k=1:length(VideoFiles)
Pattern=VideoFiles{k}(1:9);
Index=strfind(DataFiles,Pattern);
Index=cellfun(@isempty,Index);
FoundDataFile=DataFiles(~Index)
end
The for-loop is to illustrate that you can do this to all your video files. If you only need to find the matching data file for one of your video files, you can do this.
VideoFiles='SHC_LL_S1_0008_V5.5983338494.avi';
DataFiles={'2009-10-15 09-23-54_SHC_LL_S1_0008FinalData';
'2009-10-15 09-59-31_SLR_RL_S3_0008FinalData';
'2009-10-15 09-57-11_SLR_LL_S1_0008FinalData'};
Pattern=VideoFiles(1:9);
Index=strfind(DataFiles,Pattern);
Index=cellfun(@isempty,Index);
FoundDataFile=DataFiles(~Index)
I see. So with this second example I could use the currently selected item in the listbox as VideoFiles, then load all of the filenames into DataFiles{} cell array.
Pattern = VideoFiles; %set pattern
Index=strfind(DataFiles,Pattern); %search through DataFiles
Index=cellfun(@isempty,Index); %get index
FoundDataFile=DataFiles(~Index) %set the found file in FoundDataFile
%Then I can plot(FoundDataFile)
the question I have is will strfind work considering there are files named very similarly? For instance:
SHC_LL_S1_0008_V5.5983338494.avi
SHC_LL_S2_0008_V5.5983338494.avi
SHC_LL_S3_0008_V5.5983338494.avi
SHC_RL_S1_0008_V5.5983338494.avi
SHC_RL_S2_0008_V5.5983338494.avi
SHC_RL_S3_0008_V5.5983338494.avi
So technically the pattern exists to a certain extent in all of these files.
In my code, Pattern is not equal to VideoFiles. It is the first 9 characters of the VideoFiles, such as "SHC_LL_S1". For the video file names you provided, it still produces unique Pattern. As long as the Pattern is unique in your data file names, it will find the right data file. Otherwise, even human can not determine which data file is the right one, right? You then have a different problem to deal with.
Oh so thats what
Pattern=VideoFiles(1:9);
does. it sets the first 9 characters of the VideoFiles in pattern. Very nice.
actually it is
for k=1:length(VideoFiles)
Pattern=VideoFiles{k}(1:9);
that sets the first 9 characters of the VideoFiles in pattern.
Correct?
Do you think this will work?
indexselected = get(handles.listbox1,'Value');
list = get(handles.listbox1,'String');
itemselected = list{indexselected};
%so now itemselected from the listbox is the current movie
files = dir('C:\Users\ecorbett\Documents\VCoachData\0008_Capture_10152009\AccelData') %store entire datafile folder
for i = 1 : numel(files) %loop through folder
for k=1:length(files(i)) %loop through current selected file
Pattern=files{k}(1:9); %set pattern
if strfind(itemselected,Pattern) %test
%do stuff
end
end
You'll have to test it yourself. I apparently don't have all the rest stuff that you have.
Lol, good point. Im sure I can figure out from here. Thank you!!
Jan
Jan on 11 Jul 2011
@Jiang: cellfun('isempty', C) is much faster than cellfun(@isempty, C).
Thanks, Jan. You or somebody else must have reminded me before. I need to stick it into my mind.

Sign in to comment.

More Answers (1)

I would suggest an alternative:
VideoFiles={'SHC_LL_S1_0008_V5.5983338494.avi';
'RHA_RL_S3_0008_v1.59806163.avi';
'SLR_LL_S1_0008_v1.59806163.avi'};
for k=1:length(VideoFiles)
Pattern=VideoFiles{k}(1:14); %take whatever you want
matfiles=dir(fullfile('C:\placeindisc\folder',['*' Pattern '*']))
if(length(matfiles)~=1)
warning('multiple matches or no matches');
else
load(matfiles(1).name)
end
end
If you can have multiple data files per video, then add
for iter=1:length(matfiles)
load(matfiles(iter).name)
end

Categories

Community Treasure Hunt

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

Start Hunting!