Filter löschen
Filter löschen

Simple logic error I can't figure out

2 Ansichten (letzte 30 Tage)
Mike Beacham
Mike Beacham am 15 Jul. 2014
Kommentiert: Star Strider am 15 Jul. 2014
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!

Akzeptierte Antwort

Star Strider
Star Strider am 15 Jul. 2014
Try single operators for ‘|’ and ‘&’.
  2 Kommentare
Mike Beacham
Mike Beacham am 15 Jul. 2014
Thank you! What's the difference?
Star Strider
Star Strider am 15 Jul. 2014
My pleasure!
The double operators are ‘short circuit’ logical operators, and have special requirements. The single operators (for example or) perform simple logic operations.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by