How to subscribe/publish to custom messages using rosmatlab ?

Illustration
Nicolas - 2023-05-18T10:40:34+00:00
Question: How to subscribe/publish to custom messages using rosmatlab ?

I completed the getting started examples and also did some publishing/subscribing from matlab to standard ros nodes, but only for std_msgs. Now I want to use a custom message (foo_msg) from one of my package (foo), if I use rosmsg in the shell I can see it.   But if, in matlab I try to create publsiher, for a matlab node already created, typing : publisher = rosmatlab.publisher('TOPIC', 'foo/foo_msg', node); I get :     Error using rosmatlab.node/addPublisher (line 543) Java exception occurred: org.ros.exception.RosMessageRuntimeException: java.lang.ClassNotFoundException: foo.foo_msg at org.ros.internal.message.definition.MessageDefinitionReflectionProvider.get(MessageDefinitionReflectionProvider.java:58) at org.ros.internal.message.Md5Generator.generate(Md5Generator.java:44) at org.ros.internal.message.topic.TopicDescriptionFactory.newFromType(TopicDescriptionFactory.java:36) at org.ros.internal.node.DefaultNode.newPublisher(DefaultNode.java:270) at org.ros.internal.node.DefaultNode.newPublisher(DefaultNode.java:280) Caused by: java.lang.ClassNotFoundException: foo.foo_msg at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at org.ros.internal.message.definition.MessageDefinitionReflectionProvider.get(MessageDefinitionReflectionProvider.java:54) ... 4 more Therefore the publsiher is not created, the same appends for the subscriber.   Does anyone know how to add customs messages ?  

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

It sounds like you're encountering a common issue when working with custom messages in MATLAB's ROS Toolbox. The error message indicates that MATLAB cannot find the custom message definition for foo/foo_msg. Here are some steps to resolve this issue:

  1. Check ROS Package Path: Ensure that your custom message package (foo) is correctly sourced in your MATLAB environment. You can add the package path using the addpath function:

    matlab
    addpath('path_to_your_package');
    
  2. Initialize ROS: Make sure you have initialized the ROS master in MATLAB using the rosinit command:

    matlab
    rosinit;
    
  3. Verify Custom Message Definition: Ensure that the custom message definition for foo_msg is correctly generated and available in your package. You can use the rosmsg command in the MATLAB command window to list all available messages:

    matlab
    rosmsg list;
    
  4. Create Publisher: Use the rospublisher function to create a publisher for your custom message:

    matlab
    publisher = rospublisher('TOPIC', 'foo/foo_msg');
    

Here's a complete example:

matlab
% Initialize ROS
rosinit;

% Add package path
addpath('path_to_your_package');

% Create publisher for custom message
publisher = rospublisher('your_topic', 'foo/foo_msg');

% Publish a message (example)
msg = foo_msg(); % Create an instance of your custom message
send(publisher, msg);

Make sure to replace 'path_to_your_package', 'your_topic', and 'foo/foo_msg' with the actual values specific to your setup.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!