MATLAB: Matlab coder generated mex function

coderemlmatlab codermaxmexmin

Hi!
I have recently generated a mex function using Matlab coder. However, the mex function stops at one line of the code where 'max' is used. The 'max' is only one of the functions from eml toolbox. Below is the error message displayed:
If the working dimension of MAX or MIN is variable in length, it must not have
zero length at runtime.
Error in eml_min_or_max>eml_extremum (line 73)
eml_invariant(size(x,dim) > 0, ...
Error in eml_min_or_max (line 18)
extremum = eml_extremum(varargin{:});
Error in max (line 16)
maxval = eml_min_or_max('max',varargin{:});
Error in Initialization (line 78)
max_elev = max(E_32_ELEV(1,AvailPRN_init));
Thanks for your help!

Best Answer

Could it be possible that the array which is being passed to MAX has an empty dimension when you are running the MEX?
The error suggests that for a call like:
max(A)
where A is variable-sized in its first non-singleton dimension, "dim" then,
size(A,dim)
should not be zero.
As an example, suppose that A is 1-by-:5-by-10 meaning that A is variable-sized in the second dimension with an upper bound of 5 for size(A,2). In this case, "dim" would be 2 since size(A,1) is 1.
If at runtime A is 1-by-0-by-10 then you will see this error.
You could do something like:
if isempty(A)
maxVar = A;
else
maxVar = max(A);
end
in your MATLAB code to prevent this error. Alternatively, if you are specifying "dim" explicitly in your call to MAX you could use:
if size(A,dim) == 0
maxVar = A;
else
maxVar = max(A,[],dim);
end
The reason for this check in code generation is to ensure that the output of MAX has a constant value for size(max(A), dim). Functions like BSXFUN and others then depend upon this information.