how to find a string within a filename?
조회 수: 29 (최근 30일)
이전 댓글 표시
I have a list of filenames and want to search for specific strings within the filenames and have them printed horizontally separated by a ';'. for example I have the following filenames:
- apple.doc
- apple2.csv
- apple3.xls
- banana.doc
- banana2.csv
- banana3.xls
I only want the "apple" files and the final output should be: apple.doc; apple2.csv; apple3.doc
How do I do this?
댓글 수: 0
답변 (3개)
Star Strider
2014년 4월 15일
편집: Star Strider
2014년 4월 15일
Try this:
FileNames = {'apple.doc', 'apple2.csv', 'apple3.xls', 'banana.doc', 'banana2.csv', 'banana3.xls'}
NewFiles = {};
for k1 = 1:size(FileNames,2)
A = strfind(FileNames{k1}, 'apple');
if ~isempty(A)
NewFiles = [NewFiles; FileNames{k1}];
end
end
Jos (10584)
2014년 4월 25일
NAMES = {'apple.doc', 'apple2.csv', 'TESTappleTEST.xls', 'banana.doc', 'banana2.csv', 'banana3.xls','APPL_almost.txt'}
C = regexp(NAMES, '.*apple.*', 'match') % find all names with the word apple in it.
outputstr = sprintf('%s;',C{:}) ; % concatenate them in a single row
outputstr = outputstr(1:end-1) ; remove last semi-colon
disp(outputstr) % show
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!