Continue in a for loop if a file isn't present

22 次查看(过去 30 天)
I'm having a problem with the correct syntax to complete the task without receiving an error. Any help is most appreciated!
In my program there is a for loop to import a series of spreadsheets into Matlab. These spreadsheets are for every US baseball team.
I want to know the syntax of how to skip and continue the for loop if one of the files within the loop are not found in the folder.
Here is the code i have so far:
for Str = {'Diamondbacks' 'Braves' 'Orioles' 'Boston' 'Cubs' 'WhiteSox' 'Reds' 'Indians' 'Rockies' 'Tigers' 'Astros' 'Royals' 'Angels' 'Dodgers' 'Marlins' 'Brewers' 'Twins' 'Mets' 'Yankees' 'Athletics' 'Phillies' 'Pirates' 'Padres' 'Giants' 'Mariners' 'Cardinals' 'Rays' 'Rangers' 'BlueJays' 'Nationals'};
folder = '';
fileToRead1 = [Str{1} '.xls'];
sheetName='Sheet1';
// this is to organize the data in a way easy for me to use
[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
// Here is my feeble attempt to skip and continue the forloop if a file is
// not found. It doesn't work of course.
if ~isempty(fileToRead1)
newData1.data = numbers;
continue
end
end

采纳的回答

Image Analyst
Image Analyst 2012-12-22
编辑:Image Analyst 2012-12-22
Continue if the file is not there:
if exist(fileToRead1, 'file') == 0
% File does not exist
% Skip to bottom of loop and continue with the loop
continue;
end
  15 个评论
Image Analyst
Image Analyst 2021-10-18
I don't think @Clifford Shelton cares about this so rather than continue to hijack his thread more, and since this seems to be a continuing series of questions, I suggest you post your code and data in your own discussion thread. It will get answered better there.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by