Getting error Operands to the || and && operators must be convertible to logical scalar values.
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Astrik
il 31 Ago 2016
Commentato: Param Siddharth
il 23 Apr 2020
I have to check the arguments of my function for a number of cases. The function passes all tests besides the arguments
12, 3, [3 4], 5
The code is as follows
function dd= day_diff(month1, day1, month2, day2)
if month1<1 || month2<1 || day1<=0|| day2<=0 || rem(month1,1)~=0 ||rem(day1,1)~=0 || rem(month2,1)~=0 || rem(day2,1)~=0 || isscalar(month1)~=1 || isscalar(month2)~=1 || isscalar(day1)~=1 || isscalar(day2)~=1
dd=-1;
elseif (month1==2 && day1==29) || (month2==2 && day2==29)
dd=-1;
elseif (eomday(2015,month1)==30 && day1==31) ||(eomday(2015,month2)==30 && day2==31)
dd=-1;
The error I get
>> day_diff(12, 3, [3 4], 5)
Operands to the || and && operators must be convertible to logical
scalar values.
Error in day_diff (line 2)
if (month1<1 || isscalar(month1)~=1) || (month2<1
||isscalar(month1)~=1) || day1<=0|| day2<=0 || rem(month1,1)~=0
||rem(day1,1)~=0 || rem(month2,1)~=0 || rem(day2,1)~=0 ||
isscalar(day1)~=1 || isscalar(day2)~=1
Any help will be appreciated.
0 Commenti
Risposta accettata
Star Strider
il 31 Ago 2016
The short-circuit operators don’t work with vector arguments. Replace them with single ‘|’ and ‘&’ and see if that solves the problem.
0 Commenti
Più risposte (1)
Steven Lord
il 31 Ago 2016
month2 = [3 4];
z = month2 < 1
Is z a logical scalar value? In order to be a logical scalar value, it must be BOTH logical (check this with the class function) AND scalar (check with isscalar.)
Reorder your error checking to check that the input are scalar first. Once you know your inputs are scalar, then you can check their values.
1 Commento
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!