Main Content

Run Functions in Background

This example shows how to run a function in the background using parfeval and backgroundPool. When you run a function in the background, you can run other MATLAB code at the same time.

Use parfeval to run the function magic(3) and retrieve one output. Specify backgroundPool as the first argument to run the function in the background. When you use parfeval, you create a Future object.

f = parfeval(backgroundPool,@magic,1,3);

To retrieve the output from the background, use fetchOutputs. MATLAB returns the output once the execution of magic is complete.

fetchOutputs(f)
ans = 3×3

     8     1     6
     3     5     7
     4     9     2