afterEach - Run function after data is received on DataQueue - MATLAB (original) (raw)
Run function after data is received on DataQueue
Syntax
Description
[L](#mw%5F198a9f6e-cbe1-48eb-ab70-efc3edb273df) = afterEach([q](#mw%5Fbb8f1d4d-6cec-4c01-b10e-51e3fb46763f),[fcn](#mw%5F0b31dca4-d9c4-41e5-a0c0-831b43585590))
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 listenerL
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.
Examples
Automatically Process Data Sent from the Background
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.
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
Remove a Callback by Deleting the Listener
Use afterEach
to create a listener for aDataQueue
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.
Delete the listener, then send some more data to theDataQueue
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
q
— Data queue
parallel.pool.DataQueue
object
Data queue, specified as a parallel.pool.DataQueue
object.
fcn
— Callback function
function handle
Callback function, specified as a function handle. The listenerL runs the callback function after each item of data is received on the DataQueue
objectq.
The callback function must accept data
as single argument.
Example: @disp
Example: @(~)beep
Example: @(~)disp('Data received.')
Output Arguments
L
— listener
event.listener
object
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