Main Content

Run Script as Batch Job

Use batch to offload work to a MATLAB worker session that runs in the background. You can continue using MATLAB while computations take place.

Run a script as a batch job by using the batch function. By default, batch uses your default cluster profile. Check your default cluster profile on the MATLAB Home tab, in the Environment section, in Parallel > Select a Default Cluster. Alternatively, you can specify a cluster profile with the 'Profile' name-value pair argument.

job = batch('myScript');

batch does not block MATLAB and you can continue working while computations take place.

If you want to block MATLAB until the job finishes, use the wait function on the job object.

wait(job);

By default, MATLAB saves the Command Window output from the batch job to the diary of the job. To retrieve it, use the diary function.

diary(job)
--- Start Diary ---
n = 100

--- End Diary ---

After the job finishes, fetch the results by using the load function.

load(job,'x');
plot(x)

If you want to load all the variables in the batch job, use the load function without arguments.

When you have fetched all the required variables, delete the job object to clean up its data and avoid consuming resources unnecessarily.

delete(job);
clear job

Note that if you send a script file using batch, MATLAB transfers all the workspace variables to the cluster, even if your script does not use them. The data transfer time for a large workspace can be substantial. As a best practice, convert your script to a function file to avoid this communication overhead. For an example that uses a function, see Run Batch Job and Access Files from Workers.

For more advanced options with batch, see Run Batch Job and Access Files from Workers.

See Also

| |

Related Topics