Determine which cells contain elements of another cell
1 view (last 30 days)
Show older comments
I have the following cell aray:
fileNames = [{'.'}, {'..'}, {'1.bin'}, {'1_Report.txt'}, {'2.bin'}, {'2_Report.txt'}, {'3.bin'}, {'3_Report.txt'}, {'4.bin'}];
I want to determine which *.bin files have a cooresponding *.txt file, so the final result would be:
processedFiles = [{'1.bin'}, {'2.bin'}, {'3.bin'}];
or
processedIndex = [3, 5, 7];
This seems like it should be easy but i'm struggling and hoping for some help in the right direction
Mike
0 Comments
Accepted Answer
Adam Danz
on 25 Apr 2019
Edited: Adam Danz
on 25 Apr 2019
Here's a simpler solution than my first proposal.
fileNames = [{'.'}, {'..'}, {'1.bin'}, {'1_Report.txt'}, {'2.bin'}, {'2_Report.txt'}, {'3.bin'}, {'3_Report.txt'}, {'4.bin'}];
binNums = regexp([fileNames{contains(fileNames, '.bin')}], '\d+', 'match');
txtNums = regexp([fileNames{contains(fileNames, '_Report.txt')}], '\d+', 'match');
hasMatch = ismember(binNums, txtNums);
processedFiles = strcat(binNums(hasMatch), '.bin'); % = [{'1.bin'}, {'2.bin'}, {'3.bin'}];
processedIndex = find(ismember(fileNames, processedFiles)); % = [3, 5, 7];
More Answers (2)
Rik
on 25 Apr 2019
Edited: Rik
on 25 Apr 2019
A few calls to cellfun, a regexp and ismember will do the trick:
fileNames = [{'.'}, {'..'}, {'1.bin'}, {'1_Report.txt'}, {'2.bin'}, {'2_Report.txt'}, {'3.bin'}, {'3_Report.txt'}, {'4.bin'}];
bin_match=regexp(fileNames,'\.bin');%get the indices where .bin is in the filename
bin_names=fileNames(~cellfun('isempty',bin_match));%remove other files
bin_match=bin_match(~cellfun('isempty',bin_match));%remove empty elements
%find the base filename and compose the matching txt filenames
base_filename=cellfun(@(x,y) x(1:(y-1)),...
bin_names,bin_match,'UniformOutput',0);
txt_names=cellfun(@(x) {[x '_Report.txt']},base_filename);
%find out which txt file actually is in the list and keep the matching bin file
[~,b]=ismember(fileNames,txt_names);
processedFiles = bin_names(b(b~=0));
0 Comments
Adam Danz
on 25 Apr 2019
fileNames = [{'.'}, {'..'}, {'1.bin'}, {'1_Report.txt'}, {'2.bin'}, {'2_Report.txt'}, {'3.bin'}, {'3_Report.txt'}, {'4.bin'}];
binNumMatch = regexp(fileNames, '\d+.bin', 'match');
binNum = [binNumMatch{:}];
fileNumMatch = regexp(fileNames, '\d+_Report.txt', 'match');
fileNum = [fileNumMatch{:}];
ismem = ismember(cellfun(@str2double, regexp(binNum, '\d+', 'match')), cellfun(@str2double, regexp(fileNum, '\d+', 'match')));
processedFiles = binNum(ismem); % = [{'1.bin'}, {'2.bin'}, {'3.bin'}];
processedIndex = find(ismember(fileNames, processedFiles)); % = [3, 5, 7];
2 Comments
Rik
on 25 Apr 2019
Interesting how our approaches are very similar, even if this problem could be tackled in a number of ways.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!