Unable to do work through a loop of subdirectories
1 次查看(过去 30 天)
显示 更早的评论
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
2012-12-29
assignin() and genvarname() ? Uggh! Why not just toss everything into a structure, or a cell array perhaps?
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
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.
0 个评论
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.
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) ?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!