フィルターのクリア

cannot iterate over subdirectories from data structure

1 回表示 (過去 30 日間)
Lewis
Lewis 2017 年 10 月 3 日
編集済み: Stephen23 2017 年 10 月 3 日
Hi. I'm trying to create an array of subdirectories that I can then iterate over and perform some function within. Despite browsing the forums I can't seem to find a simple example of this that I can use.
Here is where I've got to:
raw_directory = 'F:\L\raw_data';
raw_structure = dir(raw_directory);
% get the directories only
isDir = [raw_structure.isdir];
raw_foldernames = {raw_structure(isdir).name};
That's great, now I can see my folders and index into whatever ones I want. Now I want to write a loop that goes into each directory and performs a function on all of the files.
Since the first 2 folders are '.' and '..', the actual folder is at position 3. But when I try to create a data structure for that I get an error:
x = dir(raw_foldernames(3))
Error using dir
Function is not defined for 'cell' inputs.
I've tried a few other things but nothing is working and would appreciate some help.

採用された回答

Stephen23
Stephen23 2017 年 10 月 3 日
編集済み: Stephen23 2017 年 10 月 3 日
You need use cell array indexing with a cell array, and generate the full path using fullfile. Note that . and .. are not guaranteed to be the first two names, and so you should remove them using setdiff or the like:
raw_directory = 'F:\L\raw_data';
raw_structure = dir(raw_directory);
raw_foldernames = {raw_structure([raw_structure.isdir]).name};
raw_foldernames = setdiff(raw_foldernames,{'.','..'});
...
F = fullfile(raw_directory,raw_foldernames{1}) % note {} not ()
S = dir(F)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by