find all anonymous function in a workspace

2 ビュー (過去 30 日間)
Leo Simon
Leo Simon 2021 年 12 月 17 日
コメント済み: Leo Simon 2021 年 12 月 17 日
There surely has to be a way of doing this: finding all anonymous functions in a specified workspace.
Presumably the solution involves using `whos` but I can't find it on web.
For a specific example suppose my workspace has the following variables
f = @x) x^2 ;
g = @(z,y) x^3 + z;
a = rand(5,3);
b = a^3 + 5;
I want a command that will identify (and return) f and g. A bonus would be for the command to list the arguments of f and g, but I can live without that.
Thanks for any suggestions.
  2 件のコメント
Steven Lord
Steven Lord 2021 年 12 月 17 日
What's your application? Why are you trying to do this / how would you use this information?
If you're trying to generate a list of possible objective functions to pass into an optimization function, an ODE solver, an integration function, etc. know that some of those functions may accept objects that have an feval method as their "function" input. So you may not be generating all the possible values available for use in that function.
Leo Simon
Leo Simon 2021 年 12 月 17 日
In poorly written code, I have many anonymous functions; in some, the only arguments are endogenous variables; in others the arguments include parameters in addition. E.g.,
f = @(x,y) a*x + b*y
g = @(x,y,a,b) a*x +b*y
I obviously don't want two functions that are intended to do the same thing, so I'm going to rewrite f as below, but first I need to find all the duplicates. Applying func2str to the elements of @Benjamin 's and @Walter Roberson answer should do the trick!
f = @(x,y) g(x,y,a,b)

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 12 月 17 日
Borrowing heavily from @Benjamin
f = @(x) x^2 ;
g = @(z,y) y^3 + z;
h = @sin; % This is NOT an anonymous function!!!!
a = rand(5,3);
b = a.^3 + 5;
S = whos();
names = {S.name};
classes = {S.class};
function_handle_names = names(strcmp(classes,'function_handle'))
info = cellfun(@(S) functions(evalin('caller', S)), function_handle_names, 'uniform', 0);
issimple = strcmp(cellfun(@(S)S.type, info, 'uniform', 0), 'simple');
anonymous_functions = function_handle_names(~issimple)
  6 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 17 日
I do not see it?
f = @(x) x^2 ;
g = @(z,y) y^3 + z;
h = @sin; % This is NOT an anonymous function!!!!
a = rand(5,3);
b = a.^3 + 5;
S = whos();
names = {S.name};
classes = {S.class};
function_handle_names = names(strcmp(classes,'function_handle'))
function_handle_names = 1×3 cell array
{'f'} {'g'} {'h'}
info = cellfun(@(S) functions(evalin('caller', S)), function_handle_names, 'uniform', 0);
issimple = strcmp(cellfun(@(S)S.type, info, 'uniform', 0), 'simple');
anonymous_functions = function_handle_names(~issimple)
anonymous_functions = 1×2 cell array
{'f'} {'g'}
Not here, and not on my desktop machine.
Remember that it is extracting information from whos() and the anonymous functions I create for info and issimple are not done until after whos() is run, so curly would have had to have been in your workspace already for it to show up.
Leo Simon
Leo Simon 2021 年 12 月 17 日
Ugh, sorry, it was in my startup script, shouidl have checked that. It's instructive though. I guess the pure way to be sure would be to run the info command before and after running the program, then do a setdiff to find the added anon functions.

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

その他の回答 (1 件)

Voss
Voss 2021 年 12 月 17 日
% set up some variables in the workspace:
f = @(x) x^2 ;
g = @(z,y) y^3 + z;
a = rand(5,3);
b = a.^3 + 5;
S = whos();
names = {S.name};
classes = {S.class};
function_handle_names = names(strcmp(classes,'function_handle'));
display(function_handle_names);
function_handle_names = 1×2 cell array
{'f'} {'g'}
  2 件のコメント
Stephen23
Stephen23 2021 年 12 月 17 日
This finds all function handles, not anonymous functions as the question requests.
f = @(x) x^2 ;
g = @(z,y) y^3 + z;
h = @sin; % This is NOT an anonymous function!!!!
a = rand(5,3);
b = a.^3 + 5;
S = whos();
names = {S.name};
classes = {S.class};
function_handle_names = names(strcmp(classes,'function_handle'));
display(function_handle_names);
function_handle_names = 1×3 cell array
{'f'} {'g'} {'h'}
Voss
Voss 2021 年 12 月 17 日
That's a good point.

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

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by