feval only for a certain variables
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
Guillaume
2015-5-14
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 个评论
Geppo Batt
2015-5-14
编辑:Geppo Batt
2015-5-14
Walter Roberson
2015-5-14
Is it possible that one of the things you are passing is a function rather than a variable?
Geppo Batt
2015-5-15
Guillaume
2015-5-15
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
2015-5-17
类别
在 帮助中心 和 File Exchange 中查找有关 Functional Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!