feval only for a certain variables
Show older comments
I computed a symbolic formula, for example fun=x^2*z^2+y^2*w^2 (in my case I have many symbolic variables) , after this I convert the symbolic formula in function handle (with "matlabFunction"), for example fun=@(x,z,y,w) x^2*z^2+y^2*w^2. I need to substitute in this function only certain variables, for example only z and w, and after other calculations substitute the other variables. Can I do this with "feval" or exist other functions?
Accepted Answer
More Answers (1)
Guillaume
on 14 May 2015
Note: I don't know anything about the symbolic toolbox, but your question seems to be only about function handles.
You only need to create a new function handle where you supply only parts of the arguments:
fun = @(x,z,y,w) x^2*z^2 + y^2*w^2;
knownz = 5; knownw = 6;
newfun = @(x,y) fun(x, knownz, y, knownw); %substitute part of the variables
%later on:
result = newfun(knownx, knowny);
Or you could create a generic function handle to perform the substitution:
substitutezw = @(z, w, fn) @(x, y) fn(x, z, y, w); %anonymous function whose output is an anonymous function
newfun = substitutezw(knownz, knownw, fun);
%later on
result = newfun(knownx, knowny)
5 Comments
Geppo Batt
on 14 May 2015
Edited: Geppo Batt
on 14 May 2015
Walter Roberson
on 14 May 2015
Is it possible that one of the things you are passing is a function rather than a variable?
Geppo Batt
on 15 May 2015
Guillaume
on 15 May 2015
I'm not sure I understand what component1 and component2 are. Are these functions of the input variable.
Can you also confirm that your last line of code is a typo? It should read:
feat_fin = feat_camera_sub(... ; %not feat_sub
Other than that typo, there is nothing wrong with the code you've posted, and testing it on my machine with some dummy variables work just fine.
Note that you have to define the anonymous function in the right order. That is component1 and component2 must be defined correctly before you declare the feat_camera anonymous function. And feat_camera_sub must be defined afterward. Once an anonymous function has been defined, changing the constants it refers to will not affect it.
Geppo Batt
on 17 May 2015
Categories
Find more on Functional Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!