%% Run Callback Function After Function Finishes Running in the Background
% This example shows how to use afterEach to schedule a callback function to run after a function finishes running in the background.
%
% Use parfeval to run the function rand(1) and retrieve one output. Specify backgroundPool as the first argument to run the function in the background. Repeat 10 times to create 10 Future objects.

for i = 1:10
    f(i) = parfeval(backgroundPool,@rand, 1, 1);
end

%
% After each Future finishes, display the value using the disp function. The input arguments for disp are the output arguments from each Future. Specify the third argument to the afterEach function as 0 to return no outputs from the callback.

afterEach(f,@disp,0);

% Copyright 2021 The MathWorks, Inc.