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 中查找有关 Author Block Masks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!