Changing function handle within a MATLAB Function block in Simulink (Coder error)
2 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Sakshi Sharma
on 8 Nov 2023
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 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!