Main Content

send

Send data to DataQueue or PollableDataQueue

    Description

    send(q,data) sends an item of data with value data to a queue q. Send data from the background or your current MATLAB® session.

    You can send data from a function that you run on a parallel pool if you have Parallel Computing Toolbox™. For details, see send (Parallel Computing Toolbox).

    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

    Input Arguments

    collapse all

    Queue, specified as a parallel.pool.DataQueue or parallel.pool.PollableDataQueue object.

    • If q is a DataQueue, use afterEach to automatically process data when it is received in the current MATLAB session.

    • If q is a PollableDataQueue, use poll to manually retrieve data after it is received in the current MATLAB session.

    Example: q = parallel.pool.DataQueue

    Example: q = parallel.pool.PollableDataQueue

    Data to send, specified as a scalar, vector, matrix, or multidimensional array.

    Example: send(q,"A message");

    Example: send(q,magic(3));

    Extended Capabilities

    Version History

    Introduced in R2017a