필터 지우기
필터 지우기

Determine which cells contain elements of another cell

조회 수: 1 (최근 30일)
Mike King
Mike King 2019년 4월 25일
댓글: Mike King 2019년 4월 25일
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.
Adam Danz
Adam Danz 2019년 4월 25일
Ha! That challenged me to think of an alternative.

댓글을 달려면 로그인하십시오.

카테고리

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