Main Content

rosmessage

Create ROS messages

Since R2019b

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").

Note

In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.

To use message structures now, set the "DataFormat" name-value argument to "struct". For more information, see ROS Message Structures.

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 type or action type of client.

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

msg = rosmessage(___,"DataFormat","struct") creates an empty message as a message structure with any of the arguments in previous syntaxes. For more information, see ROS Message Structures.

Examples

collapse all

Create a ROS message as a structure with the std_msgs/String message type.

strMsg = rosmessage("std_msgs/String","DataFormat","struct")
strMsg = struct with fields:
    MessageType: 'std_msgs/String'
           Data: ''

Start ROS master.

rosinit
Launching ROS Core...
Done in 0.52727 seconds.
Initializing ROS master on http://172.29.206.170:56782.
Initializing global node /matlab_global_node_19492 with NodeURI http://dcc598343glnxa64:43137/ and MasterURI http://localhost:56782.

Create publisher for the /chatter topic with the std_msgs/String message type. Set the "DataFormat" name-value argument to structure ROS messages.

chatpub = rospublisher("/chatter","std_msgs/String","DataFormat","struct");

Create a message to send. Specify the Data property with a character vector.

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_19492 with NodeURI http://dcc598343glnxa64:43137/ and MasterURI http://localhost:56782.
Shutting down ROS master on http://172.29.206.170:56782.

You can create an structure 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 an array of two messages. Specify the DataFormat name-value argument to use structures for ROS messages.

blankMsg = rosmessage("std_msgs/String","DataFormat","struct")
blankMsg = struct with fields:
    MessageType: 'std_msgs/String'
           Data: ''

msgArray = [blankMsg blankMsg]
msgArray=1×2 struct array with fields:
    MessageType
    Data

Assign data to individual message fields in the array.

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

Read all the Data fields from the messages into a cell array.

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

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

Note: In a future release, ROS message objects will be removed. To use ROS messages as structures and utilize structure arrays, specify the DataFormat name-value pair when calling the rosmessage function.

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 or action client, specified as a ServiceClient or ActionClient object handle. You can create a service or action client object using rossvcclient or rosactionclient respectively.

ROS service or action server, specified as a ServiceServer or ActionServer object handle. You can create a service or action server object using rossvcserver or rosactionserver respectively.

Output Arguments

collapse all

ROS message, returned as a Message object handle or a structure.

Extended Capabilities

Version History

Introduced in R2019b

expand all