cannot iterate over subdirectories from data structure
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!