Determine which cells contain elements of another cell

1 visualización (últimos 30 días)
Mike King
Mike King el 25 de Abr. de 2019
Comentada: Mike King el 25 de Abr. de 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

Respuesta aceptada

Adam Danz
Adam Danz el 25 de Abr. de 2019
Editada: Adam Danz el 25 de Abr. de 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];

Más respuestas (2)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by