MATLAB: Problem building C++ static library: “MATLAB expected a scalar”

ccode generationMATLABmatlab coder

Dear all,
I am converting some MATLAB code into a C++ static library using the MATLAB Coder. The compiler gives me an error on a line where I compare a scalar to another scalar
if Sm_user == 0
do something
end
However, MATLAB thinks that the variable scalar is in fact non-scalar and therefore the IF statement cannot be evaluated.
I have attached an image showing the pertinent code along with the build report. pair_lo, pair_hi and amps are all defined as 1x:inf arrays of class Double.
cycleMin = pair_lo(pos);
would usually be a scalar because pos is a position in the array amps. However, in the event that pos refers to more than one element in amps, cycleMin and cycleMax are redefined in the two IF statements to make sure they're scalar. Since Sa_user and Sm_user and defined from other scalars, they themselves are scalars. However, MATLAB seems to think that Sm_user is not.
How can I convince the coder that Sm_user isalways scalar?
Many thanks,
Louis Vallance

Best Answer

For anyone who also has this problem, I've solved it. It's a simple matter of pre-allocating the variables. For example:
pos = some definition % could be 1x?
if length(pos) > 1
pos2 = zeros(1, 1);
pos2(1) = pos(1);
end
Related Question