Run function after data is received on
You can only run
afterEach in the MATLAB® session where you create
If you use
afterEach to add a new listener, the listener
L still runs the function
fcn. To stop
fcn from running, use
delete the listener
After you call
afterEach, any data in the queue is
immediately processed by the listener.
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.
DataQueue object. After each item of data is received on the
DataQueue in your current MATLAB session, automatically display that item using the
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
PollableDataQueue object, then returns that magic square.
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
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
afterEach to create a listener for a
DataQueue object, then delete the listener.
DataQueue object in your current MATLAB session. Use
afterEach to display each
item of data that the
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.
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
q — Data queue
Data queue, specified as a