0

I have pretty simple question, but somehow I can't find an answer neither in google nor in matlab's help. So I have 2 folders with 200 files each, named with a certain pattern. First folder contains files, each of them has a corresponding file in the other folder. For example a pair of files:

Folder1: 2014-02-25_001_140225_111946 FFlC-1-1-100-0,55-114-315-0-3cm rms 2k.png

Folder2: Date=140225_Time=112038 FFlC-1-1-100-0,55-114-315-0-3cm avg 4k.jpg

The part FFlC-1-1...-3cm is the same for both files.

So I read directory content with dir into 2 structs, and then I can separate out the common part from the first filename into a string.

But how can I search this string among filenames from the other directory? In other words how can I search for a srting in a struct?

Filenames1=dir('....\photos1\*.png')
Filenames2=dir('....\photos2\*.jpg')

for i=1, length(Filenames1)

    string=Filenames1(i).name(30:60)
    pic= Filenames_2(find(string))  <-- but this does not work.

end

Well, actually it works, but instead of 1 filename it gives me 31 (1 correct, 30 incorrect). It looks like instead of using the whole string ('FFlC-1-1-100-0,55-114-315-0-3cm'), it takes the part before comma('FFlC-1-1-100-0').

DDR
  • 459
  • 5
  • 15

2 Answers2

0

The function you are looking for is strfind(str, pattern), and in your case, if I understand it, one idea would be:

%... loop over all elements in *Filenames1*
pattern=Filenames1(ii).name(30:60);
%... start a loop over *Filenames_2*
str=Filenames_2(jj).name;
Matches(ii,jj)=strfind(str, pattern) %don't forget to initialize *Matches*
%...
close loops

Now Matches has the positions in which the string was found or empty values if it was not.

Also this using i and j as variables in MatLab

Community
  • 1
  • 1
McMa
  • 1,568
  • 7
  • 22
  • Thanks! That was my "straight-forward" solution, but of there os no other automatic search command, I will do like this. And thanks for the i&j. – DDR May 20 '14 at 14:55
0

You can pop the file names out of the struct and in to a cell array and use strcmp, ismember, regexp or other vectorized comparisons, avoiding the inner loop. The trick is to use the { s.field } syntax to extract the .name fields from the struct array you get back from dir().

names2 = { Filenames2.name };

I would actually parse out the filenames in the second directory up front, so you can then use exact equality comparisons instead of substring searching.

Filenames1=dir('....\photos1\*.png')
Filenames2=dir('....\photos2\*.jpg')

% Extract just the names from the second dir listing
names2 = { Filenames2.name };
% parse them: fixed-width records work well as 2-D char arrays
namesTmp = char(names2);
ids2 = cellstr(namesTmp(:, 26:55));

for i=1:numel(Filenames1)
    id=Filenames1(i).name(30:60)
    % Then use exact comparison
    ix = find(strcmp(id, ids2))
    matchingFile = names2{ix}
end
Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
  • Is there any possibility to search directory straight with the pattern: `filename=dir('\...\photos2\*pattern*.jpg')`? What is the syntax of putting variable `pattern` in the directory path-string? – DDR May 20 '14 at 15:25
  • You could. The `[ ... ]` concatenation syntax works on strings too: `dir( [ '\...\photos2\*' id '*.jpg' ] )`. But you probably don't want to do that. Getting the filenames from disk once and searching in memory is a lot more efficient than doing multiple directory searches. If you want to do that substring search, just use `regexp` on the list of names you pulled out of the original `dir()` call. – Andrew Janke May 20 '14 at 15:39