Main Content

Run Script as Batch Job

Usebatchto 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 thebatchfunction. By default,batchuses your default cluster profile. Check your default cluster profile on the MATLABHometab, in theEnvironmentsection, inParallel>Select a Default Cluster. Alternatively, you can specify a cluster profile with the'Profile'name-value pair argument.

job = batch('myScript');

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

If you want to block MATLAB until the job finishes, use thewaitfunction 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 thediaryfunction.

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

After the job finishes, fetch the results by using theloadfunction.

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

If you want to load all the variables in the batch job, use theloadfunction 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); clearjob

Note that if you send a script file usingbatch, 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, seeRun Batch Job and Access Files from Workers.

For more advanced options withbatch, seeRun Batch Job and Access Files from Workers.

See Also

||

Related Topics