Questions tagged [function-handle]

In Matlab, a function handle is a mechanism provided to allow for indirect calling of a function. Serves like a "pointer to function"

A function handle is a value that provides a means of calling a function indirectly.

159 questions
4
votes
2 answers

Function handle formats in Octave

A function handles in Octave is defined as the example below. f = @sin; From now on, calling function f(x) has the same effect as calling sin(x). So far so good. My problem starts with the function below from one of my programming…
André Gomes
  • 185
  • 13
4
votes
1 answer

Create function with given number of input arguments

Here's the situation: I need to create a function that takes a function handle fun which is of CONSTANT input-length (that is nargin(fun)>=0), does some transformation on the inputs and then calls fun. Pseudo-Code: function g = transformFun(fun) n…
Omer Rosler
  • 237
  • 1
  • 10
4
votes
3 answers

Force evaluation of variables in MATLAB anonymous function

MATLAB stores variables along with anonymous functions. Here is an example of how this works from the documentation. Variables in the Expression: Function handles can store not only an expression, but also variables that the expression requires…
Marc
  • 5,315
  • 5
  • 30
  • 36
4
votes
1 answer

matlabFunction removes input arguments

I want to calculate the differentiation of a function of two variables. For example: ax^2 + by^2 + cxy So I do this: a = 1 b = 1 c = 1 syms x y f f = a*x^2 + b*y^2 + c*x*y df = matlabFunction(diff(f,'x')) which returns: df = …
soroosh.strife
  • 1,181
  • 4
  • 19
  • 45
4
votes
2 answers

How can I validate a function handle as an input argument?

I have a class that has a function handle as one of its properties. classdef MyClass properties hfun %function handle end methods function obj = Myclass(hfun,...) %PROBLEM: validate that the input argument…
joshkarges
  • 111
  • 1
  • 7
4
votes
1 answer

How to figure out eigenvalues of a matrix in matlab when all entries of matrix are variables?

I have a matrix with a bunch of unknown constants such as the one below: a*b -c -d 0 -c e -a -b-d -d -a d -e 0 -b-d -e a As you may realize it is symmetric about the diagonal and therefore,…
user972276
  • 2,973
  • 9
  • 33
  • 46
4
votes
1 answer

Octave: Compute Gradient of a Multi-Dimensional Function at a particaular point

I have been trying out the following code to find the gradient of a function at a particular point where the input is a vector and the function returns a scalar. The following is the function for which I am trying to compute gradient. %fun.m …
Hashken
  • 4,396
  • 7
  • 35
  • 51
4
votes
1 answer

Function handle error in matlab

I want to call function handle model_jacobian, but i get "Error using ==> horzcat CAT arguments dimensions are not consistent.". When i pick function that leaves both a and d coefs in jacobian, everything works fine. syms a d x; syms_function =…
3
votes
1 answer

Why does nargout return -1? And how to get the correct number of function outputs in that case?

Why does nargout return -1 in this case? function test fun=@(a)helper_fun(a,2); [x,y]=fun(1); x, % 1 y, % 2 nargout(fun), % -1 end function [c,d] = helper_fun(a,b) c=a; d=b; end Is there an alternative to extract the correct number of…
Argyll
  • 8,591
  • 4
  • 25
  • 46
3
votes
2 answers

What does a function with this ~ mean? (e.g. function=f(~, x, y))

I am doing another coursera assignemnt, this time with aerial robotics. I have to program a pd controller using the matlab ode45 (ordinary diff. equation). And the file that has to contain this code gets called as follows: pd_controller(~, s, s_des,…
Andreas K.
  • 389
  • 4
  • 17
3
votes
3 answers

Call multiple functions from cells in MATLAB

I store some functions in cell, e.g. f = {@sin, @cos, @(x)x+4}. Is it possible to call all those functions at the same time (with the same input). I mean something more efficient than using a loop.
user1666938
  • 73
  • 1
  • 9
3
votes
1 answer

Getting absolute file path from function handle

Is there possibility to retrieve the absolute path to the file containing a function represented by a function handle? For example: %child folder containing test_fun.m file handle = @test_fun cd .. %root folder - test_fun not available path =…
mitrue
  • 63
  • 6
3
votes
2 answers

Converting Cell array of function handle into a single array of function handle

I need to build up a vector of non-linear equations to be used in fsolve to solve it. But I should make each element of the vector in each loop iteration. How can I make up such a vector? In fact, I can not use cell array. How can I convert a cell…
user2745742
  • 65
  • 1
  • 4
2
votes
2 answers

Creating an anonymous function and calling it passing arguments in one line in MATLAB

You can do in matlab something like this: >> fh = @(x) x^2 fh = @(x)x^2 and then >> fh(3) ans = 9 Now I look for a way to create the anonymous function and call it in one line, like this (it does not work): @(x) x^2 (3) <-- This code does…
2
votes
2 answers

What is the correct way to declare a property of type function_handle in an abstract class in MATLAB?

I'm trying to create an interface that other classes in MATLAB will inherit. This interface has a property that holds a function_handle value. The problem I'm running into is that, upon instantiating a concrete class that inherits from this class, I…
Matt
  • 2,339
  • 1
  • 21
  • 37
1
2
3
10 11