フィルターのクリア

Make a loop with multiple conditions the correct way

2 ビュー (過去 30 日間)
Jacob
Jacob 2021 年 9 月 24 日
コメント済み: Jacob 2021 年 9 月 27 日
a = 1982
b = 3
c = 25
r = 4468
thirtydays = [4 6 9 11]
thirthyonedays = [1 3 5 7 8 10 12]
normalyears = [1971 1973 1974 1975 1977 1978 1979 1981 1982 1983 1985 1986 1987 1989 1990 1991 1993 1994 1995 1997 1998 1999 2001 2002 2003 2005 2006 2007 2009 2010 2011 2013 2014 2015 2017 2018 2019]
leapyears= [1972 1976 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020]
for t = 19820325:20201231
%try
url = sprintf('somewebsite',a,b,c);
target = 'Neerslag:' %Make sure that the webpage is in the right language form. Target should then greatly resemble the text. Wrong values might be there due to faults in the script.
data = urlfilter(url,target)
if data == -1
data = 0
end
BestStation(r,2) = data
pause(1)
% catch
% warning('weberror')
% weberror = weberror + 1
%end
r = r + 1
c = c + 1
if c > 28
while (b == 2 && a == ismember(normalyears, normalyears(1:end)))
b = b + 1
c = 1
end
end
if c > 29
while (b == 2 && a == ismember(leapyears, leapyears(1:end)))
b = b + 1
c = 1
end
end
if c > 30
while (b == ismember(thirtydays, thirtydays(1:end)))
b = b + 1
c = 1
end
end
if c > 31
while (b == ismember(thirtyonedays, thirtyonedays(1:end)))
b = b + 1
c = 1
end
end
if b > 12
a = a + 1
b = 1
c = 1
end
end
I made a loop this way so I can scrape through multiple webpages dependant on the changing dates
Something goes wrong though when the code enters the if section of this loop
I tried multiple ways of changing things around like swapping if and while, but there's something not logical about it yet (which I can not see how).
I'm using ismember now, but that doesnt seem to give the loop the magic touch.
Thank you for giving attention.
  5 件のコメント
Siddharth Bhutiya
Siddharth Bhutiya 2021 年 9 月 27 日
As Stephen already mentioned above, your life would be a lot easier if you use datetime when working with dates and times. You can get rid of all the date handling code that you have in your above example and focus on your actual task. For example I see that you have explicit code to correctly handle increments for different months and leap years, this is all automatically handled by datetime.
>> d = datetime(2020,2,28)
d =
datetime
28-Feb-2020
>> d + 1 % Its a leap year so incrementing the date by 1 day will give you 29th Feb
ans =
datetime
29-Feb-2020
>> d = datetime(2021,2,28)
d =
datetime
28-Feb-2021
>> d + 1 % 2021 is not a leap year so incrementing by 1 day will give you 1st Mar
ans =
datetime
01-Mar-2021
Jacob
Jacob 2021 年 9 月 27 日
Thanks. I thought it worked to supplement sprinft code for scraping through various pages.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by