M file in one location to loop over directories/sub directories in another location
1 回表示 (過去 30 日間)
古いコメントを表示
I'm not sure how to get this syntax to do exactly what i want it to do. Help is most appreciated!
I am trying to get my mfile to point to a different directory on my computer and loop over all the files in each subfolder. I can't get the syntax to work properly.
My M-file is located in C:/Users/Stuff
The Directory I want to point towards is: C:/Data and there are numerous subdirectories of this folder (C:/Data/041012, C:/Data/041112, C:/Data/041212.........etc.)
Here is the code I have so far that isn't working:
addpath(genpath('c:/data'))
for Str = {'Red' 'Orange' 'Yellow' 'Green' 'Blue' 'Indigo' 'Violet'};
%I don't think this next line of code is right. But i'm trying to apply the names of each associated color with the name of the file to
%import
folder = 'c:\data';
fileToRead1 = [Str{1} '.xls'];
sheetName='Sheet1';
if exist(fileToRead1, 'file') == 0
% File does not exist
% Skip to bottom of loop and continue with the loop
continue;
end
% And then begin my calculations and functions here....
0 件のコメント
回答 (2 件)
Walter Roberson
2012 年 12 月 25 日
It is best not to use addpath()
folder = 'c:\data';
wantedfiles = {'Red' 'Orange' 'Yellow' 'Green' 'Blue' 'Indigo' 'Violet'};
subdirs = dir(folder);
subdirs(~[subdirs.isdir]) = []; %eliminate non-directories
for K = 1 : length(subdirs)
thissubdir = dirs(K).name;
if strcmp(thissubdir, '.') || strcmp(thissubdir, '..'); continue; end
subdirpath = [folder '\' thissubdir];
for L = 1 : length(wantedfiles)
fileToRead1 = fullfile( subdirpath, [wantedfiles{L} '.xls'] );
if ~exist(fileToRead1, 'file'); continue; end
..... do your work ....
end
end
0 件のコメント
Image Analyst
2012 年 12 月 25 日
The genpath() function may be useful for you. It generates a list of all subfolders of a folder that you specify.
folderListCharArray = genpath('c:\Data');
folders will be separated by semicolons. I wish they'd have the option to return them individually in a cell array but they don't so you'll have to use regexp to split them apart.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!