find all anonymous function in a workspace
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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.
채택된 답변
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
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'))
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)
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.
추가 답변 (1개)
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);
댓글 수: 2
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!