Main Content

afterEach

Run function after each function finishes running in the background

    Description

    example

    B= afterEach(A,fcn,n)runs the functionfcnautomatically after each element in theFuturearrayAfinishes and returns aFutureobjectB.

    MATLAB®runs the functionfcnusing the outputs from each element inA. If theFuturearrayAhasMelements, MATLAB runs the functionMtimes. When the scheduled functionfcnfinishes for theMthtime, theFutureobjectBfinishes.

    For more information about usingafterEachto run functions after they finish running on a parallel pool, seeUse afterEach and afterAll to Run Callback Functions(Parallel Computing Toolbox).

    If any of the elements inAencounters an error, theErrorproperty ofBis a cell array with the same number of elements asA.

    B= afterEach(A,fcn,n,PassFuture=true)runsfcnusing each element inAinstead of the outputs of each element inA.

    TheErrorproperty ofBis an empty cell array, even if one or more elements inAencounter an error.

    Examples

    collapse all

    This example shows how to useafterEachto schedule a callback function to run after a function finishes running in the background.

    Useparfevalto run the functionrand(1)and retrieve one output. SpecifybackgroundPoolas the first argument to run the function in the background. Repeat 10 times to create 10Futureobjects.

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

    After eachFuturefinishes, display the value using thedispfunction. The input arguments fordispare the output arguments from eachFuture. Specify the third argument to theafterEachfunction as0to return no outputs from the callback.

    afterEach(f,@disp,0);

    This example shows how to useafterEachto update a wait bar with the progress of functions running in the background.

    Create a wait bar,w.

    w = waitbar(0,'Please wait ...');

    Set the number of iterations for yourfor-loop,N. Store the current number of completed iterations,0, and the total number of iterations,N, in theUserDataproperty of the wait bar.

    N =20; w.UserData = [0 N];

    Run afor-loop withNiterations. In each iteration, useparfevalandbackgroundPoolto runpausein the background for a random number of seconds. Store eachFutureobject in an array.

    fori = 1:N delay = rand; f(i) = parfeval(backgroundPool,@pause,0,delay);end

    Use the helper functionupdateWaitbar更新the waitbar after eachFuturefinishes.

    afterEach(f,@(~)updateWaitbar(w),0);

    Usedeleteto close the wait bar after all theFutureobjects finish.

    afterAll(f,@(~)delete(w),0);

    Define Helper Function

    Define the helper function updateWaitbar. The function increments the first element of theUserDataproperty, then uses the vector to calculate the progress.

    functionupdateWaitbar(w)% Update a waitbar using the UserData property.% Check if the waitbar is a reference to a deleted objectifisvalid(w)% Increment the number of completed iterationsw.UserData(1) = w.UserData(1) + 1;% Calculate the progressprogress = w.UserData(1) / w.UserData(2);% Update the waitbarwaitbar(progress,w);endend

    Input Arguments

    collapse all

    InputFutureobject, specified as aparallel.Futurescalar or array.

    MATLAB runs the functionfcnafter each element inAfinishes. If any of the elements inAencounters an error and you specifyPassFutureastrue,afterEach仍在运行fcnusing that element.

    • If you specifyPassFutureastrue, MATLAB runsfcn(Aj)after eachFutureelementAjinAfinishes.

      TheErrorproperty ofBis an empty cell array, even if one or more elements inAencounter an error.

    • Otherwise, MATLAB runsfcn(X1,...,Xm)using the outputsX1,...,Xmfrom eachFutureelement inAas the elements finish.

      If any of the elements inAencounters an error and you specifyPassFutureastrue,Errorproperty ofBis a cell array with the same number of elements asA.

    If theFuturearray hasMelements, MATLAB runs the functionMtimes. When the scheduled functionfcnfinishes, theFutureobjectBfinishes.

    Example:A = parfeval(backgroundPool,@magic,1,3);

    Callback function to run, specified as a function handle.

    Example:fcn = @magic

    输出参数,指定为一个nonnegative integer scalar.

    • If you specifyPassFutureastrue,nis the number of output arguments requested from runningfcn(A(j))using each elementA(j)in theFuturearrayA.

    • Otherwise,nis the number of output arguments requested from runningfcn(Y1,...,Ym)using the outputsY1,...,Ymfrom each elementAjin theFuturearrayA.

    Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

    Output Arguments

    collapse all

    OutputFutureobject, returned as aparallel.Futureobject.

    When you setPassFuture, you change theErrorproperty ofB:

    • IfPassFutureistrue,Errorproperty ofBis an empty cell array, even if one or more elements inAencounter an error.

    • Otherwise, if any of the elements inAencounters an error, theErrorproperty ofBis a cell array with the same number of elements asA.