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

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

 Accepted Answer

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 Comments

I still am getting an error.
??? Undefined function or method 'exists' for input
arguments of type 'char'.
Error in ==> baseball4Test at 26
if exists(fileToRead1, 'file') == 0
I don't know what is going wrong.
I also tried:
if exists(fileToRead1, [Str{1} '.xls']) == 0
% File does not exist
% Skip to bottom of loop and continue with the loop
continue;
end
And got the same error. Not sure what I'm doing wrong.
Use exist() instead of exists().
I make that mistake a lot myself!
That's old code. Now the way to do it is to use isfile().
if ~isfile(fileToRead1)
% File does not exist
% Skip to bottom of loop and continue with the loop
continue;
end
There is a companion function called isfolder() that checks to see if folders/directories exist.
Thanks, I missed your answer. The other code worked though. I just needed to find it again.
Thank you :D
Ok, I've tried it and found out, that thins only works for the folder I'm working in. How can I tell Matlab to check all the folders in the path?
I used this "continue if" code for my program but I need to lead my code in this direction;
if exist(fileToRead1, 'file') == 0
% File does not exist
% Skip to the next elements in loop and not going for the rest of the codes inside the loop
end
How can I arrange this for my code?
@Honey I'm not sure what you need or why my code can't be "rearranged" for what you need. Essentially it's like this:
% Read files mat1.mat through mat10.mat. Files are in the current directory.
% Use fullfile() if you need to prepend some other folder to the base file name.
for k = 1 : 10
% Create a mat filename, and load it into a structure called matData.
matFileName = sprintf('mat%d.mat', k);
if isfile(matFileName)
matData = load(matFileName);
else
fprintf('File %s does not exist.\n', matFileName);
continue; % Skip this file but continue with loop. Or use break to exit the loop.
end
% Any more code you need would go here.
end
Adapt as needed. Start a new question if you still have trouble.
Image Analyst look at this part of my long code;
When the "file1" is not exist it goes to this line " Imerg=Imerg+IMERG"
and sum the previous Imerg with again the previous IMERG while it should be empty. I need that if "file1" is not exist then it goes for the next UTM.
for yy=1:years;
year =2014+yy-1;
for month=1:12
for day=1:31
Imerg=zeros(28,59)
for UTM= 0:30:1410
if month<=9 & day<=9 & UTM<1000
file1= sprintf('3B-HHR-E.MS.MRG.3IMERG.%i0%i0%i.000%i.nc4',year,month,day,UTM)
if exist(file1) == 0
continue;
end
IMERG= ncread(file1 ,'precipitationCal')
Imerg=Imerg+IMERG
end
end
end
end
Not sure why you're not taking my suggestion. Why did you do
if exist(file1) == 0
instead of using isfile() like I suggested. If you want to check if it doesn't exist, then you can slightly alter my code like this:
if ~isfile(file1) % If not a file.... the tilde means NOT.
continue; % It does not exist. Skip it.
end
Honey
Honey on 18 Oct 2021
Edited: Honey on 18 Oct 2021
thank you so much Image Analyst but I think I am going to the wrong way.
I don't have problem with "if exist(file1) == 0" or "if ~isfile(file1)" even though I changed my code to "if ~isfile(file1)". the problem is that I put this if clouse to detect the last days of months which is not exist for example 30 and 31 of February. But when it put the values to cell, there is also data for 30 and 31 February! I think it is related to this part. "Imerg=Imerg+IMERG"
If you can please help me to solve it.
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.
Ok, you're right. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!