MATLAB Answers

Function handle restrictions for MATLAB Coder

13 ビュー (過去 30 日間)
Øystein Henriksen
Øystein Henriksen 2020 年 8 月 6 日
コメント済み: Øystein Henriksen 2020 年 8 月 12 日
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;
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!
  2 件のコメント
Øystein Henriksen
Øystein Henriksen 2020 年 8 月 6 日
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.


回答 (1 件)

Chidvi Modala
Chidvi Modala 2020 年 8 月 12 日
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);
out = x{2}(1,2);
  3 件のコメント
Øystein Henriksen
Øystein Henriksen 2020 年 8 月 12 日
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)
fcnHandle = get_function_handle(someCriteria)
someVariable = fcnHandle(someInput)
The function "get_function_handle" would itself not be an entry point function.


Community Treasure Hunt

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

Start Hunting!

Translated by