I read in the mathworks documentation (regarding MATLAB Coder) that:
In some cases, using the same bound variable to reference different function handles causes a compile-time error. For example, this code does not compile:
function y = foo(p)
x = @plus;
x = @minus;
y = x(1, 2);
I was wondering what is meant by "in some cases". Are there specific conditions for which it is allowed to store different function handles in the same variable? The reason I am asking is that I am trying to create function that selects and returns a function handle based on some criteria.
function fcnHandle = select_function(criteria, function_name)
if criteria == 1
fcnHandle = str2func(['Package1', '.', function_name]);
elseif criteria == 2
fcnHandle = str2func(['Package2', '.', function_name]);
I have 2 packages (Package1 and Package2) and they both contain a function with the exact same function signature (e.g. my_func(a,b)). So, if criteria == 1 i get the function handle for my_func(a,b) from Package1, and criteria == 2 returns the function handle for my_func(a,b) from Package2. This works fine in MATLAB, but when i try to generate code (using MATLAB Coder) i get an error saying:
Type mismatch: MATLAB Coder cannot determine the equivalence of function handles Package1.my_func versus Package2.my_func.
So my question is; can I get around this problem in an easy way? The documentation states that I am not allowed to store different function handles in the same variable "in some cases", so maybe my "case" just needs some tweaking? If it is not possible, I would highly appreciate any suggestions on how to achieve function selection in an alternative manner.
Thanks in advance!