Determine which cells contain elements of another cell

1 次查看(过去 30 天)
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

采纳的回答

Adam Danz
Adam Danz 2019-4-25
编辑:Adam Danz 2019-4-25
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];

更多回答(2 个)

Rik
Rik 2019-4-25
编辑:Rik 2019-4-25
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 2019-4-25
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 个评论
Rik
Rik 2019-4-25
Interesting how our approaches are very similar, even if this problem could be tackled in a number of ways.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by