MATLAB Answers

rosgenmsg(folderpath) from custom ros msgs add on causes Index exceeds the number of array elements (0).

1 view (last 30 days)
I'm trying to get Matlab to be able to use some custom ROS msgs from a ROS package that follows the required structure has worked in other environments. I've followed the instructions at but will get a 'Index exceeds the number of array elements (0).' Error on the rosgenmsg(folderpath)
Any one run into this issue before or is this a bug.


Daniel Levy
Daniel Levy on 30 Mar 2020
The code I used was straight from the instructions at I also made sure that folder exists.
examplePackages = fullfile(fileparts(which('rosgenmsg')), 'examples', 'packages');
userFolder = 'C:\MATLAB\CustomMessages\';
copyfile(examplePackages, userFolder)
folderpath = userFolder;
Error message is below, breaks at rosgenmsg(folderpath)
Error using ROSScratchPad (line 8)
Index exceeds the number of array elements (0).

Sign in to comment.

Accepted Answer

Cam Salzberger
Cam Salzberger on 30 Mar 2020
This kind of error message generally comes up (in any MATLAB product) when you are trying to call a function but have shadowed the function name with a variable name. For example, if you do this:
rosgenmsg = 1;
myPath = 'C:\ros';
MATLAB is trying to index into the variable "rosgenmsg" using the character values in "myPath" as index values. Since 'C' is valued at a larger index than the length of "rosgenmsg", you see this error.
Run this command to see if you have "rosgenmsg" shadowed by a variable name:
which -all rosgenmsg


Show 3 older comments
Cam Salzberger
Cam Salzberger on 31 Mar 2020
Hmm, we're now exploring odd territory. If "rosgenmsg" were indeed a variable, then I would expect to see an error message more like this:
Index exceeds the number of array elements (1).
'rosgenmsg' appears to be both a function and a variable. If this is unintentional, use 'clear rosgenmsg' to remove the variable 'rosgenmsg' from the workspace.
And this would be result of which -all rosgenmsg:
rosgenmsg is a variable.
C:\ProgramData\MATLAB\SupportPackages\R2019b\toolbox\ros\supportpackages\roscustommsg\rosgenmsg.m % Shadowed
If it was calling the correct rosgenmsg, but there was some issue with it, I'd expect to see a stack trace that shows at which line in rosgenmsg there was an issue. However, a lot of the ROS 1 functionality uses custom error messages which can sometimes disguise exactly where the error occurs in the toolbox code. Try giving this a shot, and see if the report looks more useful:
catch ME
Another thing to try after that is to simply debug-break at first point the error occurs. Sometimes this is a bit down in the weeds, but it can help situations like this. First run this:
dbstop if error
Then run your code. See where the error is occurring, what the error is, and what the workspace looks like while in debug mode.
Daniel Levy
Daniel Levy on 31 Mar 2020
So, I found the error, there was a +ros folder that got copied into another folder and was creating the duplications of functions within rosgenmsg. It wasn't getting deleted/rehashed because it was saved in the windows environment and I had to restart the computer to get rid of it.
It wasn't actually rosgenmsg that was the problem but the generate custom Jar files and some others that was throwing the error. So your original intuition was correct, I must have bungled something that copied over that +ros folder. When I was using the custom messages example's 'copyfile(examplePackages, userFolder)' command.
Thanks for the help/effort,
Cam Salzberger
Cam Salzberger on 31 Mar 2020
That's good to know, and hopefully anyone else running into the same issue can find it now. Thanks for posting the answer!

Sign in to comment.

More Answers (0)