Main Content


Generate custom messages from ROS definitions



rosgenmsg(folderpath) generates ROS custom messages in MATLAB® by reading ROS custom messages, service definitions, and action definitions in the specified folder path. The function expects one or more ROS package folders inside the folder path. These packages contain the message definitions in .msg files, the service definitions in .srv files, and the action definitions in .action files.

After calling this function, you can send and receive your custom messages in MATLAB like all other supported messages. You can create these messages using rosmessage or view the list of messages by calling rosmsg list.


  • When generating custom messages for ROS, you must build the ROS packages. This requires you to have CMake and a C++ compiler for your platform. For more information, see ROS System Requirements

  • The custom messages from the ROS definitions have to be regenerated for every new release.

rosgenmsg(folderpath,Name,Value) specifies additional options using one or more name-value pair arguments.


collapse all

After you install the support package and prepare your custom message package folder, specify the path to the parent folder and call rosgenmsg.


You must set the folderpath based on your ROS package setup.

folderpath = "C:/Users/user1/Documents/robot_custom_msg/";

Input Arguments

collapse all

Path to the parent folder of ROS message packages, specified as a string scalar or character vector. The parent folder should contain package folders. These folders contain a /msg folder with .msg files for message definitions, a /srv folder with .srv files for service definitions, and a /action folder with .action files for action definitions.

Example: "/opt/ros/noetic/share"

Data Types: char | string

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'BuildConfiguration','fasterruns'

Build configuration, specified as the comma-separated pair consisting of 'BuildConfiguration' and a character vector or string scalar containing 'fasterbuilds' or 'fasterruns'.

  • 'fasterbuilds' — Build the message libraries with compiler optimizations for shorter build times.

  • 'fasterruns' — Build the message libraries with compiler optimizations for faster execution.

Example: rosgenmsg("/opt/ros/noetic/share",'BuildConfiguration','fasterruns')

Data Types: char | string


  • ROS custom messages and the rosgenmsg function are not supported with MATLAB Compiler™.

Version History

Introduced in R2019b