Confusion about ROS2 parameters in MATLAB Coder
Show older comments
Hello everyone,
my confusion stems from the fact, that is seems not be possible to get array-valued parameters from a node. I use the following MWE-node which gets code generated with MATLAB Coder (codegen).
function myNode() %#codegen
nodeParams.gains = [0.5, 1.0, 2.0];
node = ros2node("/my_node", Parameters=nodeParams);
r = ros2rate(node, 10);
while true
[gains, ok] = getParameter(node, "gains", Datatype="double");
if ok
fprintf("First gain: %f\n", gains(1));
else
fprintf("Status is false\n");
end
waitfor(r);
end
end
This will successfully generate code, which can be compiled into a standlaone ROS2 node. Analyzing the node:
name@host:~/ros_ws$ ros2 param get /my_node gains
Double values are: [0.5, 1.0, 2.0]
Which means, that the parameter is set correct. But the node will repeatedly print:
Status is false
The code generation will also fail when actively trying to index into gains(2). I see from the Simulink-Coder documentation that there you are able to specify the datatype="double[]" which would make it clear that i want to read an array of doubles. But this is not allowed for the getParameter method. Does this mean, that it is simply not possible?
I am looking for some clarification on this topic, or possible workarounds. Thank you :)
Answers (1)
Umar
on 12 Jul 2026 at 4:45
0 votes
Hi @Sönke,
This is a real, documented limitation — not something wrong with your code. The getParameter reference page states that for the code-generation syntax, the Datatype name-value argument only supports these return types: int64, logical, string, char, or double. There's no array variant (double[], etc.) offered for this argument, unlike the Simulink equivalent.
By contrast, the Simulink Get Parameter block for ROS 2 explicitly supports array types — its Data type parameter can be set to uint8[], double[], int64[], or boolean[], and it even has a Maximum length setting specifically for sizing the array output.
So the asymmetry you found is accurate: ros2node's Parameters argument and setParameter happily accept and publish array-valued parameters (which is why ros2 param get shows your array correctly), but the MATLAB Coder-facing getParameter function was never given an array-capable Datatype option. That mismatch is almost certainly why your generated code gets status = false — the codegen path is expecting a scalar double and the type resolution fails silently at runtime rather than erroring at compile time.
Workarounds:
1. Split the array into individual scalar parameters (gain1, gain2, gain3, ...) and call getParameter once per scalar with Datatype="double". This is the most direct fix given the current API. 2. If an array parameter is a hard requirement, use Simulink + Simulink Coder instead — the Get Parameter block natively supports double[] with a configurable max length.
Worth filing this as feedback to MathWorks directly, since as of the current documentation there's no array support in the MATLAB Coder-facing getParameter function — only in the Simulink block.
See attached PDF for direct documentation references and quotes supporting this.
Categories
Find more on Network Connection and Exploration 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!