Main Content

events

Model discrete events

Parent Section: component

Syntax

events 
  when EventPredicate 
    AssignmentList 
  end 
end

Description

events begins the events section, which is terminated by an end keyword. The events section in a component file manages event updates. It is executed throughout the simulation.

The events section can contain only when clauses.

The when clause serves to update the values of the event variables. The syntax is

when EventPredicate
  var1 = expr1; 
  var2 = expr2; 
  ...
end

EventPredicate is an expression that defines when an event occurs. It must be an expression of event data type, as described in Event Data Type and edge Operator.

The variables in the body of the when clause must be declared as event variables. When the event predicate returns true, all the variables in the body of the when clause simultaneously get updated to the new values.

A when clause can optionally have one or more elsewhen branches:

when EventPredicate
  var1 = expr1; 
  var2 = expr2; 
  ...
elsewhen EventPredicate
  var1 = expr3; 
  ...
end

Note

The default else branch in a when clause is illegal.

Syntax Rules

The following rules apply:

  • The order of when clauses does not matter.

  • The order of the variable assignments in the body of a when clause does not matter because all updates happen simultaneously.

  • A when clause cannot update an event variable more than once within the same assignments list.

  • Two independent when clauses may not update the same event variable. You must use an elsewhen branch to do this.

  • The order of elsewhen branches matters. If multiple predicates become true at the same point in time, only the branch with the highest precedence is activated. The precedence of the branches in a when clause is determined by their declaration order. That is, the when branch has the highest priority, while the last elsewhen branch has the lowest priority.

Examples

expand all

This example implements a triggered delay component:

component Triggered
   inputs
      u = 0; % input signal
      triggered = 0; % control signal
   end
   variables(Event=true)
      x = 0;
   end
   outputs
      y = 0;
   end
   events
      when edge(triggered>0)
        x = u; 
      end
   end  
   equations
      y == x;
   end
end

When the control signal becomes positive, the event variable x gets updated to the current value of the input signal u. Output y outputs the value of x. Therefore, the output signal y gets updated to the current value of the input signal u on the rising edge of the control signal, and then holds that value between the events.

Related Examples

Version History

Introduced in R2016a