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

39 vues (au cours des 30 derniers jours)
Clifford Shelton
Clifford Shelton le 22 Déc 2012
Modifié(e) : Honey le 18 Oct 2021
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

Réponse acceptée

Image Analyst
Image Analyst le 22 Déc 2012
Modifié(e) : Image Analyst le 22 Déc 2012
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 commentaires
Image Analyst
Image Analyst le 18 Oct 2021
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.
Honey
Honey le 18 Oct 2021
Ok, you're right. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by