フィルターのクリア

How to use dir in a loop to be dynamic?

3 ビュー (過去 30 日間)
Rita
Rita 2015 年 8 月 20 日
コメント済み: Cedric 2015 年 8 月 20 日
Hi, Sorry for a simple question. I have abc1_0.765mat ......abc50_0.7462mat How I can call the dir in a loop.
for i=1:50
A = dir('abc1*.mat');% but it also calls abc11 and I just need abc1
end
I tried A=dir(fullfile... % but I got error
Any Suggestion

採用された回答

Cedric
Cedric 2015 年 8 月 20 日
We actually use DIR the other way around. If file names are very regular and determined by e.g. a number, we built them using SPRINTF:
for fId = 1 : 50
filename = sprintf( 'data_%02d.mat', fId ) ;
... do something ...
end
This generates names data_01.mat, data_02.mat, etc, over successive iterations. When we don't know file names, we get a directory listing using wildcards if we know a pattern, e.g.
listing = dir( 'abc*.mat' ) ;
Calling DIR once only, we get listing as a struct array whose entries contain references to all relevant files. Then we iterate over these entries:
for fId = 1 : numel( listing )
filename = listing(fId).name ;
... do something ...
end
Finally, we use FULLFILE when we need to concatenate elements of path, e.g. a folder name and a file name:
folder = 'MyData' ;
listing = dir( fullfile( folder, 'abc*.mat' )) ;
for fId = 1 : numel( listing )
fileLocator = fullfile( folder, listing(fId).name ) ;
... do something ...
end
  4 件のコメント
Rita
Rita 2015 年 8 月 20 日
Thank you so much or your great help.
Cedric
Cedric 2015 年 8 月 20 日
My pleasure.

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

その他の回答 (0 件)

カテゴリ

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