simulink.event.InputWrite - Trigger schedule event when input port value updates - MATLAB (original) (raw)

Open the model ScaleInput. The model loads external input data into a subsystem. The subsystem contains a Gain block that multiplies the subsystem input by two.

mdl = "ScaleInput"; open_system(mdl)

Create a timeseries object with input data for the model. The input data represents a line with a slope of one sampled every second for ten seconds.

sampleTime = 1; numSteps = 11; time = sampleTime*(0:numSteps-1); time = time';

data = time;

lineTS = timeseries(data,time);

Configure the model to load the timeseries object.

set_param(mdl,"LoadExternalInput","on") set_param(mdl,"ExternalInput","lineTS")

Simulate the model. By default, the maximum step size for the simulation is calculated such that the simulation takes 50 steps, resulting in a time step every 0.2 seconds.

Configure the model to execute the partition only when the value of the input port updates.

Configure the subsystem to execute as an aperiodic partition. In the Block Parameters dialog box or the Property Inspector, select Treat as atomic unit, then from the Schedule as list, select Aperiodic partition. Alternatively, use the set_param function to configure the TreatAsAtomicUnit and ScheduleAs parameters.

set_param("ScaleInput/Subsystem","TreatAsAtomicUnit","on") set_param("ScaleInput/Subsystem","ScheduleAs","Aperiodic partition")

Add an input write event trigger to the Inport block. In the Block Parameters dialog box, on the Execution tab, click Add event trigger , then select Input Write from the list. Alternatively, create a simulink.event.InputWrite object and use the set_param function to configure the EventTriggers parameter.

inputWrite = simulink.event.InputWrite; set_param("ScaleInput/Inport","EventTriggers",{inputWrite})

Update the block diagram by pressing Ctrl+D or by using the set_param function. By default, the input write event trigger is configured to trigger the Auto event. When you update the block diagram or compile the model, the software creates the event Inport.InputWrite that is scoped to the Inport block and configures the partition connected to the Inport block as the listener for the event.

set_param(mdl,"SimulationCommand","update")

Simulate the model again. Because input port data drives the partition execution, the simulation takes a step only when the input value updates.