if
Specify conditional equations, assignments, annotations, and conditional sections in component files
Parent Section: component | equations | intermediates
Syntax
ifPredicate1
Branch1
elseifPredicate2
Branch2
elseBranch3
end
Description
In the Simscape™ language, you can use if
statements in a variety of
contexts. For example, you can use if
statements to:
Specify conditional equations
Make conditional assignments when declaring intermediates
Define component variants by using conditional sections in a component file
Define conditional visibility of component members using annotations
Depending on the context, the rules and restrictions can be different.
Conditional Equations
To specify conditional equations, use if
statements within the
equations
section. An if
statement starts
with an if
keyword and ends with an end
keyword.
Every if
requires an else
.
elseif
branches are optional.
The if
and elseif
branches start with a
predicate expression. If a predicate is true, the branch gets activated. When all
predicates are false, the else
branch gets activated.
The body of each branch can contain one or more equation expressions. The total
number of equation expressions, their dimensionality, and their order must be the
same for every branch of the if-elseif-else
statement. However,
this rule does not apply to the assert
expressions, because they
are not included in the expression count for the branch.
Conditional Sections
A conditional section is a top-level section guarded by an if
clause. Conditional sections are parallel to other top-level sections of a component
file, such as declaration or equations sections.
A conditional section starts with an if
keyword and ends with
an end
keyword. It can have optional elseif
and else
branches. The body of each branch of a conditional
section can contain declaration blocks, equations, structure sections, and so on,
but cannot contain the setup
function.
The if
and elseif
branches start with a
predicate expression. If a predicate is true, the branch gets activated. When all
predicates are false, the else
branch (if present) gets
activated. The compiled model includes elements (such as declarations, equations,
and so on) from active branches only.
Unlike the if
statements in the equations section, different
branches of a conditional section can have different variables, different number of
equations, and so on.
Examples
Version History
Introduced in R2008b