How can I use "dir" with multiple search strings? or join the results of two dir calls?

32 ビュー (過去 30 日間)
Hi,
How can I use "dir" with multiple search strings? or join the results of two dir calls?
I basically want to do something like this:
dir('*.txt','*.doc')
so that I get a list of the .txt and .doc files.
Or join the results of two dir calls.

採用された回答

Walter Roberson
Walter Roberson 2011 年 2 月 28 日
jointdir = [dir('*.txt'); dir('*.doc')];
  1 件のコメント
Jan
Jan 2011 年 3 月 1 日
Most compact answer, which matchs the question exactly.

サインインしてコメントする。

その他の回答 (3 件)

Seth DeLand
Seth DeLand 2011 年 2 月 28 日
You can save the output of DIR as a structured array, or in your case two structured arrays that can then be combined:
files1 = dir('*.txt');
files2 = dir('*.doc');
allfiles = [files1;files2];
You can then access names by indexing into the array:
filename = allfiles(7).name;
This will return the name of the 7th file in the array.

Matt Tearle
Matt Tearle 2011 年 2 月 28 日
Do you just need the filenames, or do you actually need file info as well? If it's just the names, the simplest way (I think) is to use ls rather than dir:
x = cellstr(ls('*.m'));
y = cellstr(ls('*.txt'));
[x;y]

Walter Roberson
Walter Roberson 2011 年 3 月 1 日
On Unix systems, if you just need the names, the simplest way would probably be to use
[junk, allfiles] = system('ls *.{txt,doc}');
This would not work on Windows. Also, it can not be shortened to
allfiles = ls('*.{txt,doc}');
as Matlab's ls function deliberately escapes any unix meta-characters before sending them to ls.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by