Main Content


Wait for Simulink.Simulation.Job object to change state



wait(simJob) blocks execution in a client session until the job identified by the object simJob reaches the 'finished' state or fails. This occurs when all the simulations finish execution on the workers.

wait(simJob,'stateOfJob') blocks execution in the client session until the specified job object changes state to the value of 'state'. The valid states to wait for are 'queued', 'running', and 'finished'. If the object is currently or has already been in the specified state, wait is not performed and execution returns immediately. For example, if you execute wait(simJob,'queued') for a job already in the 'finished' state, the call returns immediately.

wait(simJob,'stateOfJob',timeout) blocks execution until either the job reaches the specified 'state', or timeout seconds elapse, whichever happens first.


collapse all

This example shows uses the sldemo_househeat model to show how to wait for the batch simulations to finish.

1. Open the model.


2. Define a set of values for different temperatures.

setPointValues = 65:2:85;
spv_Length = length(setPointValues);

3. Using the setPointValues, initialize an array of Simulink.SimulationInput objects.

in(1:spv_Length) = Simulink.SimulationInput('sldemo_househeat');
for i = 1:1:spv_Length 
    in(i) = in(i).setBlockParameter('sldemo_househeat/Set Point',...

4. Specify the pool size of the number of workers to use. In addition to the number of workers used to run simulations in parallel, a head worker is required. In this case, assume that three workers are available to run a batch job for the parallel simulations. The job object returns useful metadata as shown. You can use the job ID to access the job object later from any machine. NumWorkers displays how many workers are running the simulations – the number of workers specified in the 'Pool' argument plus an additional head worker.

simJob = batchsim(in,'Pool',3)
                  ID: 1
                Type: pool
          NumWorkers: 4
            Username: #####
               State: running
      SubmitDateTime: ##-###-#### ##:##:##
    Running Duration: 0 days 0h 0m 0s

5. Now, wait for the job to finish before retrieving the outputs.


Input Arguments

collapse all

A Simulink.Simulation.Job object. To create a simJob, run batchsim.

Example: simJob = batchsim(in,'Pool','5')

Value of the State property of Simulink.Simulation.Job object to wait for.

Example: wait(simJob,'queued')

Specify a timeout for wait to block execution in seconds.

Example: wait(simJob, 5)

Version History

Introduced in R2018b