- Predefine the maximum possible size: If the maximum value of “h” can be known before code-generation, then an array of that size can be allocated.
- Using “coder.varsize()”: If the size of the variable “h” can vary, but it has bounds, then an array of variable-size nature can be defined for the purpose of code-generation, in this way:
Matlab coder "All inputs must be constant"
5 views (last 30 days)
Show older comments
Hi,
I tried to generate my code to Mex and to C++ using the Matlab coder.
In my m.file I want to do the following lines:
h = round(a*b + b0);
bw_blobs = imclose((clr_cand)>0,ones(h));
When "a" and "b0" are constants and "b" is variable which change between eatch running.
When i tried to generate the code I got the next error message:
"All inputs must be constant"
how can I solve this error?
Please your advise.
Thanks,
Itai
0 Comments
Answers (1)
Abhishek
on 2 May 2025
The error you’re seeing— “All inputs must be constant” — occurs because, in code generation with MATLAB Coder, certain functions like “ones()” require their input to be a compile-time constant.
In the provided code, since “h” depends on the variable “b”, which in turn is variable, leads to this error.
Documentation for “coder.mustBeConst” confirms this argument:
"coder.mustBeConst(value)” validates that the function input value is a constant during code generation, also referred to as a compile-time constant in this context. During code generation, this function throws an error if value is not a compile-time constant.
So, using "coder.mustBeConst" would only enforce the constant requirement and cause code generation to fail.
As a workaround, the following things can be done:
coder.varsize('bw_blobs', [MIN_H, MAX_H]);
where, “MIN_H” and “MAX_H” stores the lower and upper bound for the variable “h” respectively.
The above workaround helps in a way to handle the code generation process without any error.
Please refer to the following documentation to know more about “coder.mustBeConst()”: https://www.mathworks.com/help/releases/R2024b/coder/ref/coder.mustbeconst.html
Hope this helps resolve the error.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!