When using MATLAB Code with MATLAB Coder, MATLAB Function Block, or fiaccel, the numeric type of a variable is not allowed to change under any circumstance.
Your code sets variable outputArg1 to 3 distinct numeric types which is not allowed in code generation.
However, an error can be avoided if MATLAB Coder can analyze the code and the codegen arguments and from that determine that only 1 of the three distinct type assignments is reachable.
To determine that a path is unreachable, MATLAB Coder must be able to determine that control flow variables are constant.
There are two common techniques to getting MATLAB Coder to understand that a variable is constant.
Use coder.Constant
One way is to use coder.Constant on input arguments.
fiaccel example_fi -args {coder.Constant(2)}
This will allow fiaccel to succeed, but the type of outputArg1 is always the same in the generated mex.
ProductMode: FullPrecision
Use constant variable properties for control flow
The other mechanism is to extract a property from a variable that will be constant at code generation time. The numeric type of a variable is property that as previously mentioned is constant in a (successful) code generation situation. For example.
function [outputArg1] = example_fi_two(M)
U2_0 = numerictype(0, 2, 0);
U3_0 = numerictype(0, 3, 0);
U4_0 = numerictype(0, 4, 0);
if strcmp(class(M),'double')
outputArg1 = fi(0, U2_0, 'RoundingMethod', 'Floor', 'OverflowAction', 'Saturate');
ntM = fixed.extractNumericType(M);
outputArg1 = fi(0, U3_0, 'RoundingMethod', 'Floor', 'OverflowAction', 'Saturate');
outputArg1 = fi(0, U4_0, 'RoundingMethod', 'Floor', 'OverflowAction', 'Saturate');
This will support fiaccel even if the variable M is not constant.
fiaccel example_fi_two -args {fi(pi,1,10,7)}
example_fi_two_mex(fi(3,1,10,7))
ProductMode: FullPrecision
Use multiple variables and cast to common output
Another approach is to redesign your algorithm to use separately named variables for each of the types you want to support. But you will need to cast those to some common type if you want to pass that to the output.
This function shows the general concept. And makes use of the very helpful cast-like concept.
function y = sneaky_pair_of_casts(toggle,a,b,c)
y = repmat( b(1) + c(1), size(a) );
a_cast_to_b_type = cast(a,'like',b);
a_cast_to_c_type = cast(a,'like',c);
This supports fiaccel and code generation.
fiaccel sneaky_pair_of_casts -args {true, rand(5,1), fi(pi,1,11,6), fi(pi,0,5,2) }
And will internally cast to either of two types depending on the value of the frist input argument toggle.
sneaky_pair_of_casts_mex(true, u, fi(pi,1,11,6), fi(pi,0,5,2) )
sneaky_pair_of_casts_mex(false, u, fi(pi,1,11,6), fi(pi,0,5,2) )