rosmessage

Create ROS messages

Description

example

msg = rosmessage(messagetype) creates an empty ROS message object with message type. The messagetype string scalar is case-sensitive and no partial matches are allowed. It must match a message on the list given by calling rosmsg("list").

example

msg = rosmessage(pub) creates an empty message determined by the topic published by pub.

msg = rosmessage(sub) creates an empty message determined by the subscribed topic of sub.

msg = rosmessage(client) creates an empty message determined by the service associated with client.

msg = rosmessage(server) creates an empty message determined by the service type of server.

Examples

collapse all

strMsg = rosmessage('std_msgs/String')
strMsg = 
  ROS String message with properties:

    MessageType: 'std_msgs/String'
           Data: ''

  Use showdetails to show the contents of the message

Start ROS master.

rosinit
Initializing ROS master on http://bat584805glnxa64:43157/.
Initializing global node /matlab_global_node_00452 with NodeURI http://bat584805glnxa64:38913/

Create publisher for the '/chatter' topic with the 'std_msgs/String' message type.

chatpub = rospublisher('/chatter','std_msgs/String');

Create a message to send. Specify the Data property.

msg = rosmessage(chatpub);
msg.Data = 'test phrase';

Send the message via the publisher.

send(chatpub,msg);

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_00452 with NodeURI http://bat584805glnxa64:38913/
Shutting down ROS master on http://bat584805glnxa64:43157/.

You can create an object array to store multiple messages. The array is indexable, similar to any other array. You can modify properties of each object or access specific properties from each element using dot notation.

Create a two - message object array.

msgArray = [rosmessage('std_msgs/String') rosmessage('std_msgs/String')]
msgArray = 
  1x2 ROS String message array with properties:

    MessageType
    Data

Assign data to individual object elements of the array.

msgArray(1).Data = 'Some string'; 
msgArray(2).Data = 'Other string';

Read all the Data properties from the message objects into a cell array.

allData = {msgArray.Data}
allData = 1x2 cell array
    {'Some string'}    {'Other string'}

To preallocate an array using ROS messages, use the arrayfun or cellfun functions instead of repmat. These functions properly create object or cell arrays for handle classes.

Preallocate an object array of ROS messages.

msgArray = arrayfun(@(~) rosmessage('std_msgs/String'),zeros(1,50));

Preallocate a cell array of ROS messages.

msgCell = cellfun(@(~) rosmessage('std_msgs/String'),cell(1,50),'UniformOutput',false);

Input Arguments

collapse all

Message type, specified as a string scalar or character vector. The string is case-sensitive and no partial matches are allowed. It must match a message on the list given by calling rosmsg("list").

ROS publisher, specified as a Publisher object handle. You can create the object using rospublisher.

ROS subscriber, specified as a Subscriber object handle. You can create the object using rossubscriber.

ROS service client, specified as a ServiceClient object handle. You can create the object using rossvcclient.

ROS service server, specified as a ServiceServer object handle. You can create the object using rossvcserver.

Output Arguments

collapse all

ROS message, returned as a Message object handle.

Introduced in R2019b