Script Skips a portion of code

15 views (last 30 days)
Jake
Jake on 6 Feb 2020
Edited: Jake on 31 Mar 2021
The code that I'm running is a bit lengthy and I'll post the part where I'm having the problem with. If more information is needed, I'll provide more lines of code.
%Open a data file in the same time zone
MonthBox=['C:/Folder_A/'];
Monthly=dir(MonthBox);
%Open file every month
DayCount=0;
for Monthlyi=4:length(Monthly)
% more code here
for Dailyi=4:length(Daily)
% more code here
Now, when I run the code, it skips the entire code after the "SumDayCounter=0;" . It does not enter the second "for" condition.
Can anyone think of a reason for this and a possible fix?
Apologies if the provided information is not enough. I can provide more, if needed.
Any advice is appreciated.
Thanks in advance.

Accepted Answer

Steven Lord
Steven Lord on 6 Feb 2020
If length(Daily) is less than 4, the result of the expression 4:length(Daily) will be empty and so the body of that loop will not be executed.
If you're looking to skip the files . and .., don't assume they are the first two files in the output of dir. Instead loop over the whole directory output and use continue to skip the body of the loop processing the file if the name matches one of those two special names.
  3 Comments
Steven Lord
Steven Lord on 6 Feb 2020
You can specify . or .. in file system commands on the operating systems on which MATLAB is supported to reference the current directory or the parent directory. See the table on this Wikipedia page for more information. But if you were working your way through a list of files in the directory, you probably don't want to operate on . (lest you then start iterating through all the files in that directory, which will lead you to operating on ., which will lead you to start iterating through all the files in that directory, ... see infinite loop) or on ...
People often assume that . and .. are the first two elements in the struct array returned by dir and so start processing with the third element of the output from dir. But that's not always a valid assumption. There are some directories that invalidate that assumption included in MATLAB, like the toolbox/matlab/demos directory under matlabroot (as of at least release R2019b and probably earlier releases.)
>> cd(matlabroot)
>> cd toolbox/matlab
>> D = dir('demos');
>> D(1).name
ans =
'+matlab'
>> D(2).name
ans =
'.'
>> D(3).name
ans =
'..'
For that directory, . is the second element of the output rather than the first and .. is the third rather than the second.
I guessed that you'd assumed that you could skip the first three elements in the output of dir and I wanted to let you know that was not necessarily a valid assumption. If you want to skip '.' and '..' you can iterate through all the elements of D and check if the name field contains '.' or '..' or you can filter out all the directories using the isdir field.
Jake
Jake on 7 Feb 2020
This makes perfect sense. I could fix the error, thanks to both of you!

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations 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!