MATLAB: Do I get an error when converting the MATLAB code (which utilizes “bwlabel”) to C code using MATLAB Coder

allocationdynamicmatlab codermemory

I am trying to generate C/C++ code from my MATLAB program, which utilizes "bwlabel".
However, I encounter the following error message:
??? Computed maximum size of output #1 of function 'intermediateLabelRuns' is not bounded.
Static memory allocation requires all sizes to be bounded.
The computed size is [:? x 1].
Please consider enabling dynamic memory allocation to allow unbounded sizes.
Here is an excerpt of my example code:
im_bin = zeros(dim_X,dim_Y);
coder.varsize('im_bin');
for idx=1:dim_X
for idy = 1:dim_Y
if (im(idx,idy) < threshold)
im_bin(idx,idy) = 0;
else
im_bin(idx,idy) = 1;
end
end
end
[label, nblabel] = bwlabel(im_bin, 4);

Best Answer

A possible cause of this error message Dynamic Memory Allocation During Code Generation is disabled. Please check the coder configuration to see if something similar to the following segment exists:
cfg = coder.config();
cfg.DynamicMemoryAllocation = 'Off';
codegen -config cfg ...
Replacing "Off" with one of the other choices for "DynamicMemoryAllocation" will avoid the issue.