Simple logic error I can't figure out
조회 수: 2 (최근 30일)
이전 댓글 표시
Hey, I'm making a quick file for telling if a given date is in DST or not. It's telling me
Operands to the || and && operators must be convertible to logical scalar values.
I don't know why I'm getting this.
For input values, I'm using
month =
7
5
2
day =
12
12
12
Date2 =
'2014-07-12 19:06:10'
'2014-05-12 19:06:20'
'2014-02-12 19:06:30'
My code is as follows:
if (month > 3) && (month <11)
DST=true;
elseif ((month < 3) || (month > 11))
DST= false;
elseif (month == 3)
if (day > 14)
DST=true;
elseif (day < 8)
DST=false;
else
DOW = weekday(Date2,'yyyy-mm-dd');
if (DOW == 1)
DST=true;
elseif (DOW > 1)
today = str2double(Date2(9:10));
if ((DOW == 2) && (today > 8))
DST = true;
elseif ((DOW == 3) && (today > 9))
DST = true;
elseif ((DOW == 4) && (today > 10))
DST = true;
elseif ((DOW == 5) && (today > 11))
DST = true;
elseif ((DOW == 6) && (today > 12))
DST = true;
elseif ((DOW == 7) && (today > 13))
DST = true;
else
DST = false;
end
end
end
elseif (month == 11)
if (day > 7)
DST=false;
else
DOW = weekday(Date2,'mm/dd/yyyy');
if (DOW == 1)
DST=false;
elseif (DOW > 1)
today = str2double(Date2(4:5));
if ((DOW == 2) && (today < 2))
DST = true;
elseif ((DOW == 3) && (today <3))
DST = true;
elseif ((DOW == 4) && (today <4))
DST = true;
elseif ((DOW == 5) && (today <5))
DST = true;
elseif ((DOW == 6) && (today <6))
DST = true;
elseif ((DOW == 7) && (today <7))
DST = true;
else
DST = false;
end
end
end
else
DST = false;
end
Thanks!
댓글 수: 0
채택된 답변
Star Strider
2014년 7월 15일
Try single operators for ‘|’ and ‘&’.
댓글 수: 2
Star Strider
2014년 7월 15일
My pleasure!
The double operators are ‘short circuit’ logical operators, and have special requirements. The single operators (for example or) perform simple logic operations.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!