MATLAB: Fail to create a ROS publisher/ subcriber for individual project

matlab gazeboros matlabros publisherros subsriberROS Toolbox

Hi,
I followed https://www.mathworks.com/help/ros/ug/communicate-with-the-turtlebot.html , 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!

Best Answer

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