matlabexplicitfunction-handle

Make a MATLAB function explicit


I am using a toolbox in MATLAB and I'm not ready to make any change to the functions within it.

Let's say that it has a function f = @(x,l) g(x)*h(l)

I want to call f at different x but always the same l:

l = 3;
f2(x) = @(x) f(x,l);
for i=1:length(x)
   f2(x(i));
end

However, if I do this, the function h will be called at l=3 for each i. I would like MATLAB to know when I defined f2 that h(l) can be stored and that actually f2(x) = 3*g(x)
Is there a way around this problem ? or do I need to go into the code of f2 and write somewhere:

A = h(l);
f2 = @(x) A*g(x)

Solution

  • why not define your function as?

    f2 = @(x) f(x,3)