Generically, if I have a function handle (but not the function name), is there a way to see the "help" comment block associated with that function?
Convert the handle to a string with func2str() and call help() on it:
f = #sum;
help(func2str(f))
You might need to regexp() the string, if you have an anonymous function.
Related
See two functions below. They lead to the same output [4,6], but are different in set up. Is it correct that only the first function makes use of a callback function? Is the first function preferred(more elegant?) over the other one? And is it correct that 'map' is the higher-order function in the second example as it makes use of the callback function that is within its brackets?
Thanks!
function processArray(arr,callback){
return arr.map(callback)
}
processArray([2,3], function(number){return number*2})
AND
function processArray(arr){
return arr.map(function(element){
return otherFunction(element)})
}
function otherFunction(number){
return number*2}
processArray([2,3])
Is it correct that only the first function makes use of a callback function?
No. arr.map takes a callback. You just don't see its definition here.
Is the first function preferred(more elegant?) over the other one?
Yes, but (probably) not for the reason you think. The anonymous function wrapping otherFunction is pointless. You can also write
function processArray(arr){
return arr.map(otherFunction)
}
function otherFunction(number){
return number*2
}
processArray([2,3])
And is it correct that 'map' is the higher-order function in the second example as it makes use of the callback function that is within its brackets?
In the first example, both processArray and map are higher-order functions. In the second, yes, only map is higher-order.
It's easy to inspect a function to see if it is a higher-order function: is one (or more) of its parameters invoked?
I created this function to browse all the functions stored in a file and now I want to select my function in my main program how can I select it
This is my function:
function testMode(i)
a=dir('H_*.m');
if exist('i','var')
if isempty(i)
z={a.name}';
[selection,ok]=listdlg('ListString',z,'SelectionMode','single');
if ok
i=find(selection,1,'first');
end
end
nom=a(i).name;
nom=nom(1:end-2);
disp(nom)
else
disp('fonction a un argument')
end
It looks like you will have the name of the function as a string. To call that function, you can either use feval, or you can convert the string to a function handle and call that.
result = feval(nom, argument1);
or
fcn = str2func(nom);
fcn(argument1);
The latter should be preferred, as the conversion can be done once and the function handle re-used after that, and because the calling syntax is exactly the same as calling a regular function.
For more information on function handles, see doc function_handle.
Also, for a different approach, see this document: http://www.mathworks.com/help/matlab/ref/localfunctions.html. If you can create your "menu" of functions as local functions in a single file, this approach could be very clean and simple, as it will directly give you a cell array of function handles of all of those subfunctions. From there, you can get easily get the function name for display using functions command.
My question is about the prompts that appear when you try to call a method (or a function): I'm referring to the little box that says (param1 double, param2 char, ...). I've been having trouble googling this question, mostly because I'm not sure what this box is called, so please do point me to it if this question is already answered.
If you write a function or a method and use validateattributes to specify which input types you're expecting, Matlab will pop up this box, telling you what parameters are called for when you go to call the method.
However, if you write a handle class method then you have to include the object as the first parameter. E.g.:
function [x] = doSomething(arg, param1, param2, ...)
fprintf('I did %d things!\n', param1);
end
But when you try to call this function, the popup box only asks for the arg class instance and doesn't mention the other parameters. Like this:
How can I arrange it so that my users get prompted for the correct inputs and not only the class-object itself?
Converting my comment into an answer:
The box is called function hint. The same question here did not receive an answer. I guess it is not possible currently, submit a feature request.
Suppose I want to declare some variables then declare a function:
x = 2;
function y = function(x)
y = (x^2)+1;
end
y = function(x);
disp(y)
Matlab returns the error "Function keyword use is invalid here..."
Why can't I declare variables or write any text before declaring a function? Is there good reason or is it a quirk?
EDIT:
To clarify, I do know how to get around this problem (but thanks for the suggestions nonetheless) but I suppose I'm asking why the Matlab team made this decision. By making a function declaration the first line of a file, does it have implications for memory management, or something?
The REPL prompt of Scala can have a function defined after a variable. So this is a choice (a quirk if you want) from Matlab's inner internals.
If a function is defined in a file, there are two possibilities:
The main function of that file. Then the file must begin with the function declaration: in your example, function y = fun(x). I'm using fun as the function's name. I don't think function can be used as a function's name.
See here for more details.
A nested function. In this case, the function declaration and definition can be within another function of the preceding case.
See here for more details.
As you can see, in either case the file begins with a function declaration (namely that of the main function).
The function can also be defined as an anonymous function. Then no declaration is needed, and the function can be defined anywhere. But there's a restriction: the function can contain only a single statement (so it cannot define internal variables other than the output). Therefore this method can only be used for simple functions.
In your example, the function could be defined anonymously as fun = #(x) x^2+1.
See here for more details.
Others have given good info about nested functions and such.
But the reason for the error you get is that "function" is a reserved word in Matlab. You cannot have a function with this name.
function y = my_function(x)
y = (x^2)+1;
end
And stick it in another file called my_function.m
I see a function passing like
sigma = 3*e-2
svmTrain(...,#(X,y)gaussianKernel(X,y,sigma),...);
What is going on with such a function passing, would someone explain ?
The syntax #(X,y) gaussianKernel(X, y, sigma) creates an anonymous function by binding the third argument of this existing function guassianKernel(X, y, s) to specifically be the value sigma.
If you inspect the svmTrain function signature, you'll see that it allows passing in a function, which is where this anonymous function is going.
Two things happen here:
First is function passing. For example, you had a function foo in your code, and you want to pass it as a parameter. In this case, you use # operator.
function MainScript
goo(#foo);
end
function goo(fHandle)
fHandle();
end
function foo
disp('Hello world!');
end
The second is anonymous functions. Anonymous function is a function much like every other function, except that it is defined at runtime, it has no name, and it binds to itself a local copy of variables that are passed to it. (For more information, see Closure). For example:
function MainScript
foo = #() (disp('Hello world!'));
goo(#foo);
end