MATLAB: Using vision.Cas​​cadeObjec​t​Detector on Simulink

cascadeobjectdetector;Computer Vision Toolboximage acquisitionimage analysisimage processingsimulink

Hello, I read the previous answers about the same problem but I am still not able to solve my problem. What i want is to detect water bottles and get their centroids. Here is my code,
function centroids = fcn(img)
coder.extrinsic('vision.CascadeObjectDetector');
detector = vision.CascadeObjectDetector('...\Bottle.xml');
bboxes = step(detector,img);
centroids = [bboxes(:,1)+(bboxes(:,3)/2),bboxes(:,2)+(bboxes(:,4)/2)];
And the errors are,
Unexpected MATLAB operator.
Function 'MATLAB Function' (#88.264.265), line 5, column 21:
":"
Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:Simulink | Category:Model error
Error occurred in 'Swarm_Control/MATLAB Function'.
Component:Simulink | Category:Model error

Best Answer

The issue here is that the step method of vision.CascadeObjectDetector can return a variable number of bounding boxes, from zero to (technically) infinite. See below for more info on variable-sized signals:
You can deal with this variable-size signal by either bounding the max size of the output or enabling dynamic memory allocation in your model.
A further option is to create a "wrapper" MATLAB function that uses vision.CascadeObjectDetector and always fills, for example, the N largest bounding boxes into a fixed-size output. Then, you can call that from Simulink without worrying about variable-sized signals. Instead, you have to pre-allocate that N-by-4 array of bounding boxes to, e.g., zeros and make sure you handle all the edge cases for filling that array with the output of the detector.
With this last approach you will still need to handle the variable-sized signals as above, but you will make sure that the output of your MATLAB Function block to the rest of the model is of a fixed size. It certainly helps with memory usage.
- Sebastian