I am trying to use MATLAB FUNCTION block in simulink .
In the "time_calc" Function i want to manipulate the variable "Sector" as shown in the code below
if sector == 1 || 2
sec = 1
elseif sector == 3 || 4
sec = 2
elseif sector == 5||6
sec = 3
elseif sector == 7||8
sec = 4
elseif sector == 9||10
sec = 5
elseif sector == 11 || 12
sec = 6
end
The below is the scope and you can see the values of "sector" changing from 0 through 12 and then repeating itself
But I am getting the value of "sec" as constant "1"(shown below in figure) (Maybe because it is evaluating the first "1" as boolean true and running that statement only over and over again) How to correct it ?
CodePudding user response:
if sector == 1 || 2
evaluates sector == 1
, if it's true, the statement is true. If it's false, it evaluates 2
, which is always true, and so the statement is always true.
What you intended to write was if sector == 1 || sector == 2
. You can also write this as if any(sector == [1, 2])
.
CodePudding user response:
Your function is equivalent to:
sec=ceil(sector/2)
@Cris Luengo's answer shows why your code is wrong. But I suggest you change the entire thing by this one liner, that is much clearer.
CodePudding user response:
Remove the elseif's and replace them all with just if's