Changing function handle within a MATLAB Function block in Simulink (Coder error)

6 次查看(过去 30 天)
I have a function block in Simulink that takes in an input and time and then, based on the input, chooses from a switch-case stack which function to use. The functions being chosen from are named exactly the same but held in 2 different package folders under one larger package folder. The chosen function is then evaluated and the output is returned.
The code inside the MATLAB Function Block looks like this:
function y = calc(t, option)
y = 0;
funcs = cell(2, 1);
funcs{1} = @FunctionPaths.FunctionPath1.dummy; % "dummy" is the name of the function within the FunctionPath1 package
funcs{2} = @FunctionPaths.FunctionPath2.dummy;
switch option
case 1
func = funcs{1};
case 2
func = funcs{2};
otherwise
error('Choice of "option" outside of acceptable range.')
end
y(:) = func(t);
end
The issue comes from the "funcs{2}" line: Type mismatch: MATLAB Coder cannot determine the equivalence of function handles FunctionPaths.FunctionPath1.dummy versus FunctionPaths.FunctionPath2.dummy.
How can I fix this issue?

采纳的回答

Sakshi Sharma
Sakshi Sharma 2023-11-8
Variable which holds the function handle has to be compile time constant. In this case the function handle variable is 'func'. For this to be constant the function argument 'option' has to be passed as coder.Constant.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by