MATLAB: Do I get the error “Cannot find a MATLAB message class for type /” for the custom ROS message

ROS Toolbox

I tried to generate a custom ROS message for my own package "traffic_light" with message type "Status". I installed the "ROS Toolbox Interface for ROS Custom Messages" support package and tried to follow the steps from the following documentation pages:
My folder structure looks like this:

I ran the "rosgenmsg" command on the 'my_message' parent folder to generate the custom message:
>> folderpath = 'C:\MyDocuments\my_message\';
>> rosgenmsg(folderpath)
I tried to follow all the indicated steps, but after restarting MATLAB I get the following error:
ERROR: >> rosmessage('traffic_light/Status')
Cannot find a MATLAB message class for type traffic_light/Status.

Best Answer

  • Please try out the following steps for troubleshooting:
    1) Custom message folder must be on MATLAB path
    As a first step, make sure that your custom message appears in the list returned by the command:
    >> rosmsg list
    If this is not the case, it could mean that your custom message folder is not on the MATLAB path. You can achieve this by running the commands:\n
    Then, restart MATLAB and confirm that the message appears in "rosmsg list", as shown below:
    2) Generated JAR File must be on Java class path
    If the "rosmessage" command still fails, the next step is to make sure that the generated JAR file is contained in the MATLAB Java class path. To check this, enter the command:\n
    >> javaclasspath
    At the very bottom of the list, there should be an entry pointing to your custom message folder:\n\n
    If this is not the case, you need to create a javaclasspath.txt file. Use the following commands:\n\n
    >> cd(prefdir)
    >> edit('javaclasspath.txt')
    If you get the following prompt that the file doesn't exist and whether it should be created, click "Yes":
    Add the full path to the generated JAR file on a new line in the file:\n
    Then, save the file and restart MATLAB. Now, the JAR file should appear on your javaclasspath, as shown below:
    3) Check spelling
    Finally, ensure that you are using the correct spelling for your custom message when calling "rosmessage". For example, there should be no white spaces between the <package> and <type>.\n
    % Incorrect rosmessage calls:
    >> rosmessage('traffic_light / Status')
    >> rosmessage('traffic_light/status')
    Once you double-checked the spelling, the "rosmessage" command should succeed:\n\n
    >> rosmessage('traffic_light/Status')
    ans = 
      ROS Status message with properties:
        MessageType: 'traffic_light/Status'
              Color: ''
           Duration: 0
           Latitude: 0
          Longitude: 0
      Use showdetails to show the contents of the message