Anonymous function to class property method.

I have a situation somewhat like the following mockup:
absfoo.m
classdef (Abstract) absfoo
methods (Abstract)
[f] = objfun(o, x)
[c, ceq] = confun(o, x)
end
end
confoo.m
classdef confoo < absfoo
methods
function [f] = objfun(o, x)
f = sin(sum(x.^2));
end
function [c, ceq] = confun(o, x)
c(1) = cos(0.1+x(1).^2);
c(2) = x(1)+x(2);
ceq = [];
end
end
end
bar.m
classdef bar
properties
myfoo
end
methods
function runme(o)
x0 = [0 1];
fmincon(@o.myfoo.objfun,x0,[],[],[],[],[],[],@o.myfoo.confun)
end
end
end
runscr.m
clear all
mf = confoo()
b = bar();
b.myfoo = mf;
b.runme()
Running runscr results in an error message similar to this:
Undefined function or variable 'o.myfoo.objfun'.
Error in fmincon (line 536)
If I add a wrapper method in bar, I can work around this error -- but I'd like to be able to do without the wrapper.
Edited to add commas to fmincon argument list and also empty lb, ub arguments per Steven's correction.

 Accepted Answer

Note: you shouldn't call your class bar as that already has a meaning in MATLAB.
Use this:
fmincon(@(x) objfun(o.myfoo, x),x0,[],[],[],[],[],[],@(x) confun(o.myfoo, x))
The object o.myfoo isa confoo while x will be a double array. This means confoo is the dominant class. By the procedure listed here MATLAB will call the objfun method of the confoo class to call when fmincon tries to evaluate the anonymous objective function. The same holds for confun.
In addition to changing the objective and nonlinear constraint functions I had to add commas between the A, b, Aeq, and beq inputs as well as adding the lb and ub inputs as empty inputs.

5 Comments

Thanks a bunch! That is just what I was looking for. Thanks also for the poitner to the relevant doc, though I'm not sure I would have sorted it from that.
My actually problem is a little different in that I currently pass parameters to these functions by passing them 'beyond' the options argument of fmincon (is that still documented/supported?). So I was slightly avoiding the (x) after the @. However, it won't be any problem to go ahead and use the more official way of passing parameters to the anonymous function directly.
Sorry for the errors in my example, I didn't really mean for anyone to try to execute it. I've corrected the fmincon argument list in case anyone ever comes back to this question at a later date. I also promise to not name anything in my code foo and bar. (:
Extra arguments has not been documented since about MATLAB 5.0
That checks out. I pretty much learned Matlab circa v5.0. Of course, I still think of anonymous functions as a cool 'new' feature.
I recommend against using the "beyond the options argument" approach for a couple reasons.
First, as Walter said that approach hasn't been documented in a very long time. Older functions (like fmincon) still accept that approach mainly for backwards compatibility purposes.
Second, using anonymous functions or one of the other techniques given in the "Parameterizing Functions" page to which the fmincon documentation page links allows you to pass those parameters only into the functions that use them. If you were to specify the parameters after the options, all the functions (objective, nonlinear constraint, and any that you specify in the options structure itself like the OutputFcn and PlotFcn) that you pass into fmincon must accept all those parameters regardless of whether or not they use them.
Finally, newer (relatively speaking) functions, especially those introduced since release R14 back in 2004 (which introduced anonymous functions) likely will not accept that approach.
Also because of the way that it examines arguments to deduce which calling sequence you are using, there are circumstances under which it can confuse an extra parameter as being one of the regular parameters. This is documented deep in the comments of the code.

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!