Function handle restrictions for MATLAB Coder

12 Ansichten (letzte 30 Tage)
Øystein Henriksen
Øystein Henriksen am 6 Aug. 2020
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
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
Øystein Henriksen
Øystein Henriksen am 6 Aug. 2020
Thanks for your suggestion!
I tried creating a cell array of function handles and returning the correct cell based on my criteria, but I get the same error. I think the issue is that the function handles refer to different functions, and thus cannot be referenced by the same variable. This will also be an issue (when using MATLAB Coder) with your second suggestion.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Chidvi Modala
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
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" ?
Øystein Henriksen
Øystein Henriksen am 12 Aug. 2020
I would be calling the get_function_handle-function inside all my entry-point functions. The structure would be like this:
function entry_point_func_1(something)
<code>
fcnHandle = get_function_handle(someCriteria)
someVariable = fcnHandle(someInput)
<code>
end
The function "get_function_handle" would itself not be an entry point function.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by