2
1

More than 5 years have passed since last update.

dirでファイルだけ取り出したい!

Last updated at Posted at 2019-02-08

Matlabってなぁに?

数学や科学の研究者に人気がある(らしい)。
これわかんないなーってググっても全然引っかからなくて泣きたくなる言語/開発環境。

私はファイルごとに処理をしたかった

「ディレクトリのファイルパス渡すから、中に入ってるファイル全てにこの処理やってね」っていうあぁそれどこにもよくあるよねってことをやりたかった。
とりあえず手始めに100ファイル入ってるフォルダの中身をとってこよう。

main.m
>> listing = dir('C:¥target_directory¥100_files_inside¥');

listing =

 フィールドをもつ 102×1  struct 配列
 
 name
 folder
 date
 bytes
 isdir
 datenum

100ファイルなのに102…?
cmdでも似たようなことが起こりますね。
余計な2はカレントディレクトリ( . )と親ディレクトリ( .. )でした。

ファイルだけ取り出すには「ディレクトリじゃない」を使おう

さてさて答え合わせ。
上記のstruct配列にある「isdir」、これはディレクトリならtrue、ファイルならfalseを返すので、これを使っていきましょう。

main.m
>> listing = dir('C:¥target_directory¥100_files_inside¥');

(中略)

>> listing = listing(~[listing.isdir]);

listing =

 フィールドをもつ 100×1  struct 配列
 
 name
 folder
 date
 bytes
 isdir
 datenum

これでisdirがfalse(~)の要素のみ取り出せましたね、めでたしめでたし。
あとは煮るなり焼くなりclearするなりお好きにどうぞ。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1