how to find a string within a filename?

20 Ansichten (letzte 30 Tage)
andrew
andrew am 15 Apr. 2014
Beantwortet: Jos (10584) am 25 Apr. 2014
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?

Antworten (3)

Sara
Sara am 15 Apr. 2014
Try looking up the strfind command.

Star Strider
Star Strider am 15 Apr. 2014
Bearbeitet: Star Strider am 15 Apr. 2014
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
  1 Kommentar
andrew
andrew am 25 Apr. 2014
I used this but it returns a double '18' how do I get the string of the doubles

Melden Sie sich an, um zu kommentieren.


Jos (10584)
Jos (10584) am 25 Apr. 2014
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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by