Script Skips a portion of code

9 ビュー (過去 30 日間)
Jake
Jake 2020 年 2 月 6 日
編集済み: Jake 2021 年 3 月 31 日
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.

採用された回答

Steven Lord
Steven Lord 2020 年 2 月 6 日
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 件のコメント
Steven Lord
Steven Lord 2020 年 2 月 6 日
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 2020 年 2 月 7 日
This makes perfect sense. I could fix the error, thanks to both of you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by