Determine which cells contain elements of another cell

1 vue (au cours des 30 derniers jours)
Mike King
Mike King le 25 Avr 2019
Commenté : Mike King le 25 Avr 2019
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

Réponse acceptée

Adam Danz
Adam Danz le 25 Avr 2019
Modifié(e) : Adam Danz le 25 Avr 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];
  1 commentaire
Mike King
Mike King le 25 Avr 2019
Did exactly what i wanted, Thanks Adam!

Connectez-vous pour commenter.

Plus de réponses (2)

Rik
Rik le 25 Avr 2019
Modifié(e) : Rik le 25 Avr 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));

Adam Danz
Adam Danz le 25 Avr 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 commentaires
Rik
Rik le 25 Avr 2019
Interesting how our approaches are very similar, even if this problem could be tackled in a number of ways.
Adam Danz
Adam Danz le 25 Avr 2019
Ha! That challenged me to think of an alternative.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by