Fail to create a ROS publisher/ subcriber for individual project

22 views (last 30 days)
I followed , and applied this idea to my specific project in my robot, not to the Turtlebot.
Basically, I have my own ROS project, and successfully built it in the Virtual Machine- Ubuntu-Gazebo 9.
After I used roslaunch to start my own project in VM, and in the Matlab I used rosinit(ipaddress) to successfully intialized Matlab global node and connected with ROS master,
and I successfuly got the names of all rostopic by using the command rostopic list in Matlab command window.
Now, I am trying to create a new publisher in Matlab to publish information to one topic (name as /mytopic, for example) in my own ROS project by using command: Matlab_Pub = rospublisher("/mytopic").
But I got an error: Failed to create a ROS publisher with topic name /mytopic and message type mytopic_msgs/MotorCmd.
And I also got the same issue when trying to creat a new subscriber in Matlab to subscribe to one topic in my own ROS project: Matlab_Sub = rossubscriber("/mytopic")
Could you please give me some insight on this issue and how to fix them?
Thank you so much for your help!
Van Chuong Nguyen
Van Chuong Nguyen on 5 Feb 2021
Edited: Van Chuong Nguyen on 5 Feb 2021
Thanks Josh, so I have some thoughts
3. Is this a message already existed in the network? (e.g. published from or subscribed by a node in your virtual machine)
==> Yes, this message is already existed in my VM. I can run the project separately in the VM, without Matlab in host computer. But now I want to use Matlab to connect with my ROS project in VM, writing some codes in Matlab to control the robot in the Gazebo.
2. Instead of rosinit(ipaddress), if you do "rosinit", and then create such publisher or subscriber, would that work?
==> After I do "rosinit", and type: Matlab_Pub = rospublisher("/mytopic"), It shows that "Cannot determine the message type of the /mytopic topic. Either the topic is not registered with the ROS master, or the ROS master is not reachable". ==> It looks like I need to set up the the ROS network in Matlab first before doing the rospublisher?
1. What is this "mytopic_msgs/MotorCmd"? Is it a custom message? If yes, have you created a custom message using rosgenmsg?
==> So it is a type of message in my ROS project in VM. I have not created any message using rosgenms. Btw, could you please let me know what does a custom message you mentioned mean? and why/ how/ is it neccessary we use that for my specific project?
Since I am new to the ROS toolbox, and not sure the way to do it properly, so your advice would be really helpful!
Thank you

Sign in to comment.

Accepted Answer

Josh Chen
Josh Chen on 5 Feb 2021
Edited: Josh Chen on 5 Feb 2021
Hi Van,
Based on your reply, it did seem that you are missing custom message definition in MATLAB. So MATLAB has a set of predefined ROS messages (e.g. std_msgs/Float64) that you can directly use for publisher and subscriber. a full list of those predefined messages can be seen by typing in
>> rosmsg list
However, in this case, when you tried to publish or subscribe to a topic that has a message which does not shipped with MATLAB, MATLAB needs more information about such message. Once you provide the message definition files (*.msg) and run the rosgenmsg, you should be able to see it in the rosmsg list. After that, you should be able to use that message for publisher and subscriber.
Here you can find more about ROS custom message and here is a documentation page showing an example of generating a custom message in MATLAB.
Hope this helps,
Josh Chen
Josh Chen on 5 Feb 2021
Edited: Josh Chen on 5 Feb 2021
Hello Van,
Yes, that is correct, the custom message should be the same type with the message of /mytopic. You can probably copy the message definition files from your project.
I would suggest to:
  • play around the doc example and make sure you are able to generate the custom message as shown in the example
  • copy your custom message and generate them following the same way you did before (make sure it shows up in >> rosmsg list)
  • then have fun using publisher and subscriber. :)

Sign in to comment.

More Answers (0)


Find more on Get Started with ROS Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by