Is it possible to declare a anonymous function globally?

16 次查看(过去 30 天)
Hello,
I have a main script-file and various subfunctions (in separate files). In the main script I have declared some anonymous functions like this (but more complex):
test = @(x, y) x * y - x;
test(5, 6)
= 25
How can I globalize these anonymous functions to use them within my separate subfunction-files?
Right now I have to setup all the anonymous functions in each subfunction-file again and again but whenever I adjust one of the anonymous functions, I have to do this for every file. This is quite annoying.
Thank you!

采纳的回答

Stephen23
Stephen23 2017-5-3
编辑:Stephen23 2017-5-3
Here are some options that avoid using globals:
1) Put them each into their own Mfiles.
2) Put them into one cell array, and pass that cell array properly as a function argument:
>> C = {@sin,@cos,@sqrt}; % functions in cell array
>> C{2}(pi) % call second function
ans =
-1
3) Put the functions into one non-scalar structure, and pass that structure properly as a function argument
>> S(1).test = @sin;
>> S(2).test = @cos;
>> S(3).test = @sqrt;
>> S(2).test(pi) % call the second function
ans =
-1
4) (Walter Roberson's suggestion): Put them into the fields of a scalar structure:
>> T.sin = @sin;
>> T.cos = @cos;
>> T.sqrt = @sqrt;
>> T.cos(pi) % call the "cos" function
ans =
-1
  4 个评论
Manuela Gräfe
Manuela Gräfe 2017-5-3
Thanks for your answer and comments, but regarding option #1 I have a further question.
Here is my adjusted example:
a = 7.554;
b = 12.678;
c = 12.678;
d = 1243.678;
e = 999.678;
festFunction = @(x, y) x * y - x + a + b * sqrt(c) - (d / e);
festFunction(5, 6)
= 76.4514
That's the matter: There are more variables (a, b, c, d, e) in the equation and I would like to avoid always passing a so many parameters to the function.
Any idea for another workaround for this?
By the way: Using my anonymous functions like this (first defining the anonymous function and then the parameter a – e) doesn't work either:
festFunction = @(x, y) x * y - x + a + b * sqrt(c) - (d / e);
a = 7.554;
b = 12.678;
c = 12.678;
d = 1243.678;
e = 999.678;
festFunction(5, 6)
This gives me this error:
Undefined function or variable 'a'.
Error in test>@(x,y)x*y-x+a+b*sqrt(c)-(d/e)
Thank you!
Stephen23
Stephen23 2017-5-3
编辑:Stephen23 2017-5-3
If you put the functions into their own Mfiles then you will need to either
  1. pass all those arguments and return the calculated value, or
  2. return a function handle that accepts those arguments.
At the point when a function handle is created those variables either need to have a value, or be specified as an input argument (or be symbolic, but I would not recommend that for your case).
One convenient and simple way of passing many arguments is to put them into one scalar structure.
I do not understand what your question has to do with method #1, as your example uses an anonymous function and does not seem to use an Mfile.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by