Working with ROS Messages in MATLAB

Overview

In ROS, messages serve as the primary data containers for communication. They are utilized by topics and services to transfer information between nodes. Every message posesses a specific type that defines its data structure, such as sensor_msgs/LaserScan for laser scanner data. MATLAB provides robust support for a wide array of ROS message types commonly used in robotics. This guide demonstrates how to create, inspect, and populate ROS messages within the MATLAB environment.

Identifying Message Types

First, initialize the ROS master and a global node:

rosinit

Populate the network with a sample topology using the helper function:

exampleHelperROSCreateSampleNetwork

List the active topics in the network:

rostopic list

To inspect the data type associated with a specific topic, such as /scan, use the info command:

rostopic info /scan

You can create a blank message object for a specific type using rosmessage. The 'DataFormat','struct' argument returns the message as a MATLAB structure:

laserScan = rosmessage("sensor_msgs/LaserScan", "DataFormat", "struct")

Accessing Message Properties

ROS messages in MATLAB are represented as structures or objects. You can subscribe to a topic to receive data. For instance, subscribing to the /pose topic:

poseSub = rossubscriber("/pose", "DataFormat", "struct")

Retrieve the latest message with a timeout of 10 seconds:

poseData = receive(poseSub, 10)

The received message is of type geometry_msgs/Twist. You can access nested fields directly to inspect values:

poseData.Linear
poseData.Angular

To extract a specific value:

xCoord = poseData.Linear.X

For a comprehensive overview of the message structure, use rosShowDetails:

rosShowDetails(poseData)

Populating Message Data

You can create a new message and modify its fields. Since we are using the struct format, modifications are straightforward assignments.

motionCmd = rosmessage("geometry_msgs/Twist", "DataFormat", "struct");
motionCmd.Linear.Y = 5;
motionCmd.Linear

Here is an example of creating a node, publishing modified twist messages, and visualizing the variance of the data:

% Initialize node and publisher
deviceNode = ros2node("/sensor_sim");
velPub = ros2publisher(deviceNode, "/cmd_vel", "geometry_msgs/Twist");

% Pre-allocate message array
msgArray(10) = motionCmd;

for idx = 1:10
    % Refresh message fields
    msgArray(idx) = motionCmd;
    
    % Simulate sensor reading with noise
    msgArray(idx).Linear.Y = 20 + randn * 3;
    
    % Calculate variance after collecting enough samples
    if idx >= 5
        msgArray(idx).Linear.X = var([msgArray(1:idx).Linear.Y]);
    end
    
    % Publish to network
    send(velPub, msgArray(idx))
end

% Visualize
errorbar([msgArray.Linear.Y], [msgArray.Linear.X])

Persisting Messages to Disk

MATLAB allows you to save message structures to MAT files for later analysis.

% Receive fresh data
poseData = receive(poseSub, 10);

% Save to file
save('robot_pose.mat', 'poseData');

% Clear variable from workspace
clear poseData;

% Load data back
loadedData = load('robot_pose.mat');
loadedData.poseData

Tags: MATLAB ROS Robotics sensor_msgs geometry_msgs

Posted on Mon, 12 Oct 2026 16:05:07 +0000 by gotissues68