MATLAB: Do the custom services or messages begin with the incorrect ROS message type in MATLAB but not in a terminal

custommessagesmsgRobotics System Toolboxrosrosgenmsgservicessrv

I have the following folder structure for my custom messages in the package folder core_msg:
/user_folder/core_msg/msg
/user_folder/core_msg/srv
/user_folder/core_msg/package.xml
Then, to create the files MATLAB needs to use the custom services and message from core_msg, I call rosgenmsg with the path to user_folder.
After setting up MATLAB to use my custom services and messages, when I call 
rosservice info /NameOfService
from inside MATLAB on my custom service /NameOfService defined in /user_folder/core_msg/srv, I get the following error:
Cannot find a MATLAB message class for type core_srv/NameOfService.
However, when I use the same command from a terminal, I am able to successfully get information, including the Type, which is correctly identified as core_srv/NameOfService.
I have also noticed that in matlab_gen\+robotics\+ros\+custom\+msggen\CustomMsgConsts.m, all of my services begin with the wrong message type (core_msg) instead of the expected one (core_srv).
Why do my custom services or messages begin with the incorrect ROS message type in MATLAB but not in a terminal?

Best Answer

This is because all of the msg and srv files are in one folder (core_msg) with one package.xml file (for the package core_msg). As a result, as far as MATLAB is concerned, when you use rosgenmsg, there is only one package (core_msg). MATLAB then uses this package name to start the ROS message type for all custom services and messages.
To let MATLAB know that the MessageType for the services should start with 'core_srv', perform the following:
(1) Move the srv folder into a new folder 'user_folder/core_srv' and create a package.xml for the package 'core_srv' in this folder. 
(2) Keep the msg folder in 'user_folder/core_msg' and modify the dependencies in package.xml, if necessary.
At this point, the folder structure should look like the following:
/user_folder/core_msg/msg
/user_folder/core_msg/package.xml
/user_folder/core_srv/srv
/user_folder/core_srv/package.xml
(3) Execute rosgenmsg with the path to user_folder.
(4) Do the necessary setup to use the generated custom messages.
Related Question