Unable to do work through a loop of subdirectories

조회 수: 2 (최근 30일)
Clifford Shelton
Clifford Shelton 2012년 12월 29일
I'm having mucho problems trying to get this to work: I'm trying to do some work while looping through a group of sub-directories.
I am not getting any error messages, but I am not able to complete the plotting that I am trying to do for a group of excel files that are located in various folders that are all sub-folders of the C:\data folder.
Any help is MOST appreciated. I have been stumped on this for a good part of the week! Here is the code that I have so far:
echo on;
folder = 'c:\data';
wantedfiles = {'Angels' 'Diamondbacks' 'Orioles' 'Royals' 'Yankees' 'Mets' 'Giants'};
subdirs = dir(folder);
subdirs(~[subdirs.isdir]) = [] ;
numberOfFolders = length(subdirs);
if numberOfFolders <= 0
uiwait(warndlg('Number of folders = 0!'))
end
for K = 1 : numberOfFolders
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'] );
sheetName='Sheet1';
if exist(fileToRead1, 'file')== 0
continue;
end
[numbers, strings, raw] = xlsread(fileToRead1, sheetName);
if ~isempty(numbers)
newData1.data = numbers;
end
if ~isempty(strings) && ~isempty(numbers)
[strRows, strCols] = size(strings);
[numRows, numCols] = size(numbers);
likelyRow = size(raw,1) - numRows;
% Break the data up into a new structure with one field per column.
if strCols == numCols && likelyRow > 0 && strRows >= likelyRow
newData1.colheaders = strings(likelyRow, :);
end
end
% Create new variables in the base workspace from those fields.
for i = 1:size(newData1.colheaders, 2)
assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i))
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
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 12월 29일
assignin() and genvarname() ? Uggh! Why not just toss everything into a structure, or a cell array perhaps?
Walter Roberson
Walter Roberson 2012년 12월 29일
What happens when you use the debugger to step through the program? What part seems to be going wrong ?

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

답변 (3개)

Matt J
Matt J 2012년 12월 29일
편집: Matt J 2012년 12월 29일
Since all the wantedfiles seem to have distinct names, why not just put the entire directory tree under c:\data on the MATLAB path
addpath(genpath('c:\data'))
and remove it later if desired.
If everything under c:\data is on the path, you can just read each file by specifying its raw name alone, without prepending the file's full directory path address.

Image Analyst
Image Analyst 2012년 12월 29일
편집: Image Analyst 2012년 12월 29일
Your code won't find .xls files that are more than one subfolder deep. Is that the problem? If so, Matt's suggestion of addpath(genpath) will fix that. Though, I'd not use addpath, just use genpath, and construct the full path as I go down the list of folders - it's just a difference in style/preference. If you use addpath() and want to remove it afterwards like he said, you can use
rmpath(genpath('c:\data'));
Otherwise, if that's not the problem, say exactly what the problem is. Does it hang/never finish/infinite loop? What exactly does "not able to complete the plotting" mean?
Oh yeah! One other quirk that you wouldn't be aware of at first, until it bites you. The upper left cell of the three cell arrays you get from xlsread() DO NOT ALL START AT THE SAME EXCEL CELL. This is unexpected, at least by me. So you can't just put Excel in R1C1 mode and read off the row and column number and expect to find your data in that row and column number of your cell array. I can hear you saying "What!?!?" - well it's true. Each starts at the upper left cell of where it's data type starts in the worksheet. So if you have a blank row 1, and a blank column1, and column headers in going from B2 to H2, and strings going down column B and the numbers in C3 to H50, then cell (1,1) of the string and raw cell arrays corresponds to cell B2 of your workbook and cell (1,1) of your numbers cell array corresponds to cell C3 - a completely different cell in Excel. So don't think that you can just say strings(4,5) and numbers(4,5) and think they both refer to the same cell E4 in Excel, because they don't - neither one does (in my example). Take time to understand this!
I also second Walter's guidance not to use assignin. There's no reason why you can't reuse the same variable. If you need to save them for use outside the loop for some reason, put them into different cells of a cell array.
  댓글 수: 1
Clifford Shelton
Clifford Shelton 2012년 12월 29일
Ok thanks for the help everyone. Sorry if I'm not clear as to what "won't work".
When I am debugging the program it seems to just ignore completely my commands to plot a graph of the two variables 'Score' and 'Allow'.
I am not sure as to why the program I wrote just seems to ignore the plot commands.
After trying to debug the code I listed above..I'm still in the dark. If the files I'm trying to import are being located, then why won't the plot be created?
And if the files are not being located, then why won't I get an error when I call the command to plot the data??
Very confused!

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


Matt J
Matt J 2012년 12월 29일
편집: Matt J 2012년 12월 29일
It's not clear from the code you've shown what the connection is supposed to be between the variables Score/Allow and the data in your files. You don't assign the data from the files to these variables anywhere that we can see.
In any case, it seems likely that Score and Allow are []. That would cause PLOT to ignore them. Did you examine the contents of Score and Allow before plotting them, (e.g., using OPENVAR or displaying them directly at the command line or at the K>> line) ?

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by