Thread Subject: function handles

Subject: function handles

From: Bernd Bumüller

Date: 21 Mar, 2010 23:15:05

Message: 1 of 5

Hello!
I have list with parameters, with which I calculate a result which is in the end processed in my main file. Like this:

[Code]

function [a,b]=parameters;
a=1;
b=2;
 
[/Code]


[Code]

function some=sum
[a,b]=parameters;
some=a+b;
 
[/Code]


%main file
[Code]

   .
   .
   .
someFunction(sum) %the result is processed here
   .
   .
   .
 [/Code]



Now I wanted to use more than one list of parameters which only needed to be passed to sum.m . Somewhere I read that this is done with function handles. So I tried this:

[Code]

function [a,b]=parameters2;
a=3;
b=4;
 
[/Code]

function some=sum(function_handle)
lalala=@function_handle;
[a,b]=lalala;
some=a+b;
 


%main file
[Code]
   .
   .
   .
someFunction(sum(parameters2))
   .
   .
   .
 [/Code]
 


But this didn't work.Can anyone please post some correct code for this?
Thank you.

Subject: function handles

From: Jan Simon

Date: 22 Mar, 2010 00:23:04

Message: 2 of 5

Dear Bernd!


> Now I wanted to use more than one list of parameters which only needed to be passed to sum.m . Somewhere I read that this is done with function handles. So I tried this:
>
> function [a,b]=parameters2;
> a=3;
> b=4;

> function some=sum(function_handle)
> lalala=@function_handle;
> [a,b]=lalala;
> some=a+b;

> someFunction(sum(parameters2))

> But this didn't work.
It is always redommended not to claim "this does not work", but post the exact error message and the corresponding line of code. Nobody can run your code, so it is impossible to guess, what exactly happens.

You could try this: Call your function SUM with a function handle:
  someFunction(sum(@parameters2))
Then the "lalala" is not needed.
But it could be easier to apply your calling method and use VARARGIN to handle a different number of inputs.

BTW: Do not use the name of builtin functions as "sum" as names for functions or variables. You can find a lot of postings here, which have been solved by avoiding these names.

Good luck, Jan

Subject: function handles

From: Bernd Bumüller

Date: 22 Mar, 2010 12:27:02

Message: 3 of 5

"Jan Simon" wrote

> It is always redommended not to claim "this does not work", but post the exact error message and the corresponding line of code. Nobody can run your code, so it is impossible to guess, what exactly happens.
>
> You could try this: Call your function SUM with a function handle:
> someFunction(sum(@parameters2))
> Then the "lalala" is not needed.
> But it could be easier to apply your calling method and use VARARGIN to handle a different number of inputs.
>
> BTW: Do not use the name of builtin functions as "sum" as names for functions or variables. You can find a lot of postings here, which have been solved by avoiding these names.
>
> Good luck, Jan

Hallo Jan,
great answer, it worked with varargin. But i am still interested in how it should look like with function handles. Your suggestion:

[Code]
function some=anyfun(function_handle)
 [a,b]=function_handle;
 some=a+b;
[/Code]
[Code]
 someFunction(anyfun(@parameters2))
[/Code]

brings the error message: anyfun.m, line 2, column 5
"function_handle" was previously used as a variable,
conflicting with its use here as the name of a function

I would be happy about any new suggestions!

Subject: function handles

From: Steven Lord

Date: 22 Mar, 2010 13:17:43

Message: 4 of 5


"Bernd Bum端ller" <leiwies@gmx.de> wrote in message
news:ho7nmm$qvh$1@fred.mathworks.com...
> "Jan Simon" wrote
>> It is always redommended not to claim "this does not work", but post the
>> exact error message and the corresponding line of code. Nobody can run
>> your code, so it is impossible to guess, what exactly happens.
>>
>> You could try this: Call your function SUM with a function handle:
>> someFunction(sum(@parameters2))
>> Then the "lalala" is not needed.
>> But it could be easier to apply your calling method and use VARARGIN to
>> handle a different number of inputs.
>>
>> BTW: Do not use the name of builtin functions as "sum" as names for
>> functions or variables. You can find a lot of postings here, which have
>> been solved by avoiding these names.
>>
>> Good luck, Jan
>
> Hallo Jan,
> great answer, it worked with varargin. But i am still interested in how it
> should look like with function handles. Your suggestion:
>
> [Code]
> function some=anyfun(function_handle)
> [a,b]=function_handle;
> some=a+b;
> [/Code]
> [Code]
> someFunction(anyfun(@parameters2))
> [/Code]
>
> brings the error message: anyfun.m, line 2, column 5
> "function_handle" was previously used as a variable,
> conflicting with its use here as the name of a function
>
> I would be happy about any new suggestions!

Bernd,

I _think_ I understand what you're trying to do, but if I'm wrong you should
start at the beginning and explain what exactly you're trying to accomplish.
Your original posting, when I reread it, sounds more like you have decided
upon an approach and want to know how to implement it, but there may be an
alternate approach that's easier to understand, more robust, and/or more
efficient.

If you want to invoke a function handle with 0 inputs, you need to call it
using empty parentheses:

function some = anyfun(function_handle)
[a, b] = function_handle();

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: function handles

From: Bernd Bumüller

Date: 22 Mar, 2010 17:44:04

Message: 5 of 5

Thank you, it was the parentheses. Now it is working.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
function handle Bernd Bumüller 21 Mar, 2010 19:19:05
rssFeed for this Thread

Contact us at files@mathworks.com