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

23 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Image Analyst
Image Analyst il 22 Dic 2012
Modificato: Image Analyst il 22 Dic 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 Commenti
Image Analyst
Image Analyst il 18 Ott 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 il 18 Ott 2021
Ok, you're right. Thank you!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by