필터 지우기
필터 지우기

Can't seem to loop through subdirectories and do work...

조회 수: 1 (최근 30일)
Clifford Shelton
Clifford Shelton 2012년 12월 27일
I'm attempting to loop through excel files that are contained in a bunch of folders that are all subdirectories of the c:\data folder.
To test to see if my loop is working, I am trying to generate plots for each imported file that should be executed in my forloop.
However, I cant' get the script to execute anything. I don't get any errors, but the script is not doing exactly what I want to complete.
Any help would be MOST appreciated! Here is the example code:
folder = 'c:\data';
wantedfiles = {'Angels' 'Diamondbacks' 'Orioles' 'Royals' 'Yankees' 'Mets' 'Giants'};
subdirs = dir(folder);
subdirs(~[subdirs.isdir]) = [];
for K = 1 : length(subdirs)
thissubdir = subdirs(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
subplot (2,1,1), plot(Score,Allow)
title('Testing to see if it works');
subplot (2,1,2), plot(Allow,Score)
title('Well, did it?');
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2012년 12월 27일
Although you loop through, you do not read the content of the files.
Anyhow, use the debugger to step through and see if anything unexpected is happening.
  댓글 수: 1
Image Analyst
Image Analyst 2012년 12월 27일
편집: Image Analyst 2012년 12월 27일
And add this line to the top of your code:
echo on;
and take the semicolon off the ends of your line. If your code is executing then something should certainly print to the command window if you do those two things. Then split this line
for K = 1 : length(subdirs)
up like this:
numberOfFolders = length(subdirs)
if numberOfFolders <= 0
uiwait(warndlg('Number of folders = 0!'));
end
for K = 1 : numberOfFolders

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by