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
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!