Anonymous function alternative for MATLAB Coder?
5 views (last 30 days)
I'm using fmincon and would like to export my code to C++ via MATLAB Coder. However, my objective function uses more arguments than just the variable being optimised. To allow this, I use an anonymous function handle for my objective function:
MATLAB Coder does not support anonymous functions. What alternative can I use to pass additional objective function arguments that MATLAB Coder supports?
Muthu Annamalai on 28 Jul 2015
Edited: Muthu Annamalai on 28 Jul 2015
You may write out your function with extra arguments as object.
First define this class,
function obj = FcnWithArgs(a,b,c)
obj.a = a;
function yval = invoke(obj,xval)
yval = original_function( xval, obj.a, obj.b, obj.c )
Next you can make an instance of this class and use it with fmincon This could be codegen compatible. Worth a try.