Main Content

afterEach

Run function after data is received on DataQueue

Description

L = afterEach(q,fcn) returns a listener that runs the function fcn after each item of data is received on the DataQueue object q.

You can only run afterEach in the MATLAB® session where you create q.

If you use afterEach to add a new listener, the listener L still runs the function fcn. To stop the function fcn from running, use delete to delete the listener L.

After you call afterEach, any data in the queue is immediately processed by the listener.

example

Examples

collapse all

This example shows how to automatically process data in your current MATLAB session that you send from the background.

Create a DataQueue object. After each item of data is received on the DataQueue in your current MATLAB session, automatically display that item using the disp function.

q = parallel.pool.DataQueue;
afterEach(q,@disp);

The helper function magicWithSend defined at the end of this example sends the sum of a magic square to a DataQueue or PollableDataQueue object, then returns that magic square.

Use parfeval and backgroundPool to run the function magicWithSend in the background.

f = parfeval(backgroundPool,@magicWithSend,1,q,3);

The sum is displayed before you fetch outputs from the future. To retrieve the output from the background, use fetchOutputs. MATLAB returns the output once the execution of magicWithSend is complete.

fetchOutputs(f)
ans = 3×3

     8     1     6
     3     5     7
     4     9     2

Define Helper Function

Define the helper function magicWithSend. The function creates a magic square, then sends the sum of the magic square to a DataQueue or PollableDataQueue object. After the sum is sent, the function returns the magic square.

function X = magicWithSend(q,n)
    X = magic(n);
    s = sum(X,'all');
    send(q,s);
end

Use afterEach to create a listener for a DataQueue object, then delete the listener.

Create a DataQueue object in your current MATLAB session. Use afterEach to display each item of data that the DataQueue object receives.

q = parallel.pool.DataQueue;
L = afterEach(q,@disp);

Send some data to the DataQueue object. When the data is received, the listener displays the data.

send(q,magic(3))
     8     1     6
     3     5     7
     4     9     2

Delete the listener, then send some more data to the DataQueue object. When you delete the listener, it stops displaying data that you send to the DataQueue object.

delete(L)
send(q,magic(3))

Input Arguments

collapse all

Data queue, specified as a parallel.pool.DataQueue object.

Callback function, specified as a function handle. The listener L runs the callback function after each item of data is received on the DataQueue object q.

The callback function must accept data as single argument.

Example: @disp

Example: @(~)beep

Example: @(~)disp('Data received.')

Output Arguments

collapse all

Listener, returned as an event.listener object.

The listener runs the function fcn after each item of data sent to the DataQueue object q is received in the current MATLAB session. To stop the function fcn running, delete the listener.

Version History

Introduced in R2017a