Evaluating function handles without writting inputs

Hi. I want to write a code that call some function handles from another code. after loading them,I want to evaluate them (means that substitute the amount of inputs in function handles) accoeding to inputs introduced at the first lines of code.
because the number of function handles is large, I prefer to call and evaluate them only with the name of functions not by name and inputs, although the inputs have introduced before (all functions have inputs). a simple example is:
x=5;y=10;
f=@(x,y) x.^2+y.^2;
now evaluate f only with calling name not inputs. for example
eval(f)
Does anyone have any suggestions for this؟

7 件のコメント

John D'Errico
John D'Errico 2024 年 4 月 6 日
Why do people so desperately want to use crap tools like eval, global variables and goto constructs? The first two are tools I fervently wish had never been included in the language. The last is one we did manage to escape. All of them are just tools that make your code worse, encouraging lazy but terribly poor coding habits.
Is there a way? Not really, at least not easily. Can you do it, by creating a custom class? Then you can define eval to work in any way you want for that class. So probably yes, in a sense. But then f is not going to be a function handle.
Paul
Paul 2024 年 4 月 6 日
Hi pooya azizi,
Please clarify the meaning of "evaluate f only with calling name not inputs"
Assuming that such an operation is feasible, what would be the expected result?
Stephen23
Stephen23 2024 年 4 月 6 日
編集済み: Stephen23 2024 年 4 月 6 日
@pooya azizi: if you want to call a function with no inputs, then why did you define them with input arguments?
Questions: do all of the functions use exactly the same inputs in the same order?
"Does anyone have any suggestions for this"
Don't do this.
pooya azizi
pooya azizi 2024 年 4 月 6 日
編集済み: pooya azizi 2024 年 4 月 6 日
all of my functions have inputs but for simply writting of the code (because the large numbers of functions), I prefer to evaluate them only with name (and the amount of inputs defined before).
my meaning of evaluating is to substitutes inputs in functions.
any function has different inputs from others
Stephen23
Stephen23 2024 年 4 月 6 日
@pooya azizi: if you want help with this then please answer my question.
pooya azizi
pooya azizi 2024 年 4 月 6 日
I answered your questions. Please look above
Shadow
Shadow 2024 年 4 月 9 日
Why don't you store your data in a structure, that can be passed around?
data.x = 3;
data.y = 2;
f = @(data) data.x^2 + data.y^2;
f(data)
ans = 13

サインインしてコメントする。

回答 (1 件)

Rik
Rik 2024 年 4 月 9 日

0 投票

The only way I can think of would be by wrapping the anonymous functions.
x=5;y=10;
f=@(x,y) x.^2+y.^2;
f=@() f(x,y);
phi = 1.5*pi;
g=@(phi) sin(phi)/(2*cos(phi));
g=@() g(phi);
feval(f)
ans = 125
feval(g)
ans = 2.7219e+15
If the idea is to cache the inputs, you should use normal functions with persistent variables:
x=5;y=10;
f_(x,y)
f_
ans = 125
function z=f_(x,y)
persistent p
if nargin>0
p.x = x;
p.y = y;
return
else
if isempty(p)
error('initialise function first')
end
x = p.x;
y = p.y;
end
z = x.^2+y.^2;
end

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

製品

リリース

R2016b

質問済み:

2024 年 4 月 6 日

回答済み:

Rik
2024 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by