Main Content

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

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 associated with client.

msg = rosmessage(server) creates an empty message determined by the service 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.87184 seconds.
Initializing ROS master on http://192.168.0.10:53250.
Initializing global node /matlab_global_node_74952 with NodeURI http://bat800205glnxa64:37397/

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_74952 with NodeURI http://bat800205glnxa64:37397/
Shutting down ROS master on http://192.168.0.10:53250.

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 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 or a structure.

Compatibility Considerations

expand all

Behavior change in future release

Extended Capabilities

Introduced in R2019b