Function handle restrictions for MATLAB Coder
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
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;
if p
x = @minus;
end
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]);
end
end
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!
2 Kommentare
Walter Roberson
am 6 Aug. 2020
I wonder what would happen if you used
function fcnHandle = select_function(criteria, function_name)
fcnHandles = {str2func(['Package1', '.', function_name]), str2func(['Package2', '.', function_name]) };
fcnHandle = fcnHandles{criteria};
end
or if you used
function fcnHandle = select_function(criteria, function_name)
fcnHandle = str2func(sprintf('Package%d.%s', criteria, function_name));
end
Antworten (1)
Chidvi Modala
am 12 Aug. 2020
The example from the documentation shows a single variable being reassigned to a different function handle. However, the other case refers to using cell arrays. Since cell arrays support data of different types, assigning each element of a cell array to a different function handle doesn't give a compile time error. You can make use of the following example
function out = foo(p)
x = cell(2,1);
x{1} = @minus;
x{2} = @plus;
if p
out = x{1}(1,2);
else
out = x{2}(1,2);
end
end
3 Kommentare
Walter Roberson
am 12 Aug. 2020
I would be concerned about whether you would be violating " Do not pass function handles to or from entry-point functions" ?
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!