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
2
votes
1 answer

Derivative of anonymous functions without defining symbolic variables in Matlab

Consider the following code: f = @(x) x.^2; Is it possible to get the derivative of the function handle f as another function handle, without defining a symbolic variable?
Msh
  • 192
  • 2
  • 7
2
votes
1 answer

Matlab parsing at run-time of function handles

My Question: Given a function handle, does matlab parse the string each time it needs to evaluate it, or just once and then it caches it? Example Consider the ingenious function function [] = foo(func) for j=1:1e4 func(j); end and the…
Amir Sagiv
  • 376
  • 5
  • 23
2
votes
1 answer

Get the handle of a function of a class that has no instantiation yet

I am fairly new to C#. What I want to do may seem convoluted. Let's start by saying that I want to take a handle of some functions in order to execute them later on. I know that I can achieve this in the following way: List list = new…
Vaaal88
  • 591
  • 1
  • 7
  • 25
2
votes
2 answers

How to access function handles in a cell array?

rate_arr_cst_1 = @(t) 2*sin(t)+10; rate_arr_cst_2 = @(t) 3*sin(2*t)+8; rate_arr_cst_h = {rate_arr_cst_1, rate_arr_cst_2}; I defined a cell array in such way and try to access in the following way: i=1; h = rate_arr_cst_h(i); but what I get here…
Loading Zone
  • 177
  • 2
  • 12
2
votes
1 answer

fminsearch error: DOUBLE cannot convert the input expression into a double array

I am encountering a problem during an optimization exercise. I am trying to use fminsearch() in matlab to solve it. The following error is generated when the code is run: The following error occurred converting from sym to double: Error using…
pvl
  • 194
  • 1
  • 10
2
votes
1 answer

Function Handles in Julia

What is the standard way to define a callback function, or a function handle in Julia? Suppose I define function myFun(a, b, c, d) a - 3* b - c * d # The return value end My goal is to fix b = 1, c = 2, d = 3, and pass myFun as a function of…
user25004
  • 1,868
  • 1
  • 22
  • 47
2
votes
2 answers

How to create an array of functions in matlab?

I was trying to solve Lorenz System in matlab by using the method of RK2 and RK4. I had a script for both of the methods, the problem now is how can converge the following y(1) = @(t,y) 10*(y(2)-y(1)); y(2) = @(t,y) y(1)*(28-y(3))-y(2); y(3) =…
Lechen Yuan
  • 23
  • 1
  • 1
  • 3
2
votes
1 answer

Trouble with anonymous functions in matlab

I am having trouble with printing out h_a_b. I am able to get functions f and g but not this one. I need to use h_a_b function so i can do h(f(x),g(x)) and calculate the sqrt of h(a,b). see equations I am always getting this error Undefined function…
2
votes
2 answers

Errors when using functions with multiple inputs

I am trying to evaluate a duffing oscillator in MATLAB with multiple inputs, and I am getting an odd error I was hoping someone might be able to help me with. Here is the code: % file duffing.m function dy=duffing(t,y,a,b,d,g,w) dy=[y(2);…
user188764
  • 23
  • 3
2
votes
2 answers

`localfunctions` inside a package

localfunctions returns function handles to all the local functions in an m-file. However, this doesn't work in a package. For example, the following code saved as 'a.m' runs fine: function fs = a() fs = localfunctions; end function…
Memming
  • 1,731
  • 13
  • 25
2
votes
2 answers

MATLAB optimization: speed up computation on large matrices

I am using the following function: kernel = @(X,Y,sigma) exp((-pdist2(X,Y,'euclidean').^2)./(2*sigma^2)); to compute a series of kernels, in the following way: K = [(1:size(featureVectors,1))', kernel(featureVectors,featureVectors,…
Eleanore
  • 1,750
  • 3
  • 16
  • 33
2
votes
3 answers

how can I pass an argument to a function handle, but interpret it only on call time?

What I want to do is: a = 5 foo = @(x) x+a a = 3 foo(1) % recieve 4 Instead, I only get 6! On several other tests I ran, I get that a is evaluated when foo is, and and not when foo is called. For various reasons, I can't work with foo = @(x,a)…
Amir Sagiv
  • 376
  • 5
  • 23
2
votes
1 answer

MATLAB: Function Handle of Nested Function

Is there a way to create a function handle to a nested function that includes the parent function in the function handle? As an example, say I have: function myP = myParent() myP.My_Method = myMethod; function myMethod() disp…
Jomnipotent17
  • 451
  • 7
  • 23
2
votes
2 answers

nargin on matlab class method returns -1

I have the following scenario. In myClass.m I have defined classdef myClass ... methods function y = foo(this, x) ... end end end Then I execute obj = myClass(); nargin(@obj.foo) and get as a result -1 while…
kon psych
  • 626
  • 1
  • 11
  • 26
2
votes
1 answer

Cell Array and function handle

Look at the below code: for i=1:2 if i == 1 F{i}= @(x) x(i)+x(i+1); else F{i}= @(x) x(i-1)-x(i)+2; end end I wanted to have stored in F something like F={@(x) x(1)+x(2);@(x) x(1)-x(2)+2;}. How should I edit my code to achieve this? Can…
user2745742
  • 65
  • 1
  • 4
1 2
3
10 11