필터 지우기
필터 지우기

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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by