Thread Subject:
What does @(src, evnt) in addlistener?

Subject: What does @(src, evnt) in addlistener?

From: Kelvin

Date: 8 Aug, 2012 18:17:34

Message: 1 of 8

Hi pros:

I was trying to use Event to catch let say buy stock signal. However, when I checked the syntax for Event I found @(src, evnt) in the scripts. Does anybody know what it means?

Besides, the scripts below are two static methods in a class called BackTesting but the function buyStock was not executed while Event 'HitBuyPriceEvent' was triggered. Could you tell me why?

========================================
function addBuyStock(stock)
  addlistener(stock, 'HitBuyPriceEvent', @(src, evnt)BackTesting.buyStock(obj, src));
end
        
function buyStock(obj, stock)
  stock.Code
  stock.Name
  obj.transactionIndex
  obj.transactionIndex = obj.transactionIndex + 1
end
========================================

Thank you in advance

Subject: What does @(src, evnt) in addlistener?

From: Bruno Luong

Date: 8 Aug, 2012 18:36:15

Message: 2 of 8

"Kelvin " <kelvinho8@hotmail.com> wrote in message <jvuafu$bb2$1@newscl01ah.mathworks.com>...
> Hi pros:
>
> I was trying to use Event to catch let say buy stock signal. However, when I checked the syntax for Event I found @(src, evnt) in the scripts. Does anybody know what it means?

Search for "anonymous functions".

Bruno

Subject: What does @(src, evnt) in addlistener?

From: Kelvin

Date: 9 Aug, 2012 04:38:15

Message: 3 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jvubiv$f93$1@newscl01ah.mathworks.com>...
> "Kelvin " <kelvinho8@hotmail.com> wrote in message <jvuafu$bb2$1@newscl01ah.mathworks.com>...
> > Hi pros:
> >
> > I was trying to use Event to catch let say buy stock signal. However, when I checked the syntax for Event I found @(src, evnt) in the scripts. Does anybody know what it means?
>
> Search for "anonymous functions".
>
> Bruno


Thank you for your reply, Bruno. If the function we want to trigger has more than one input parameter like my case function BuyStock(obj, src), do we have to add one more input parameter to @(src, evnt) to be @(obj, src, evnt)?

Thank you in advance - Kelvin

Subject: What does @(src, evnt) in addlistener?

From: Bruno Luong

Date: 9 Aug, 2012 08:48:15

Message: 4 of 8

"Kelvin " <kelvinho8@hotmail.com> wrote in message <jvvern$2np$1@newscl01ah.mathworks.com>...

>
> Thank you for your reply, Bruno. If the function we want to trigger has more than one input parameter like my case function BuyStock(obj, src), do we have to add one more input parameter to @(src, evnt) to be @(obj, src, evnt)?

No. Two input parameters is what the listener callback would expect.

I'm not sure OBJ supposes to be, and nobody but you can answer that.

Bruno

Subject: What does @(src, evnt) in addlistener?

From: Kelvin

Date: 9 Aug, 2012 09:34:14

Message: 5 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jvvtgf$dlt$1@newscl01ah.mathworks.com>...
> "Kelvin " <kelvinho8@hotmail.com> wrote in message <jvvern$2np$1@newscl01ah.mathworks.com>...
>
> >
> > Thank you for your reply, Bruno. If the function we want to trigger has more than one input parameter like my case function BuyStock(obj, src), do we have to add one more input parameter to @(src, evnt) to be @(obj, src, evnt)?
>
> No. Two input parameters is what the listener callback would expect.
>
> I'm not sure OBJ supposes to be, and nobody but you can answer that.
>
> Bruno


Hi Bruno,

If only two input parameters is what the listener call back would accept, then are you saying the function triggered by the listener could only has one input parameter? e.g. BuyStock(src)?

Is is possible for the function triggered by the listener having more than one input parameter?

Thank you - Kelvin

Subject: What does @(src, evnt) in addlistener?

From: Bruno Luong

Date: 9 Aug, 2012 09:44:12

Message: 6 of 8

"Kelvin " <kelvinho8@hotmail.com> wrote in message <k0006m$l5i$1@newscl01ah.mathworks.com>...

> If only two input parameters is what the listener call back would accept, then are you saying the function triggered by the listener could only has one input parameter? e.g. BuyStock(src)?

No. I never say that.

>
> Is is possible for the function triggered by the listener having more than one input parameter?

No.

Bruno

Subject: What does @(src, evnt) in addlistener?

From: Christopher Creutzig

Date: 9 Aug, 2012 14:21:17

Message: 7 of 8

On 09.08.12 11:34, Kelvin wrote:

> Is is possible for the function triggered by the listener having more than one input parameter?

What would the other parameters be?

Of course, you can always fix parameters by creating a function like
@(evt) MyCallBack(evt,3,4,otherData).


HTH,
Christopher

Subject: What does @(src, evnt) in addlistener?

From: Steven_Lord

Date: 9 Aug, 2012 14:34:43

Message: 8 of 8



"Kelvin " <kelvinho8@hotmail.com> wrote in message
news:jvvern$2np$1@newscl01ah.mathworks.com...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> <jvubiv$f93$1@newscl01ah.mathworks.com>...
>> "Kelvin " <kelvinho8@hotmail.com> wrote in message
>> <jvuafu$bb2$1@newscl01ah.mathworks.com>...
>> > Hi pros:
>> >
>> > I was trying to use Event to catch let say buy stock signal. However,
>> > when I checked the syntax for Event I found @(src, evnt) in the
>> > scripts. Does anybody know what it means?
>>
>> Search for "anonymous functions".
>>
>> Bruno
>
>
> Thank you for your reply, Bruno. If the function we want to trigger has
> more than one input parameter like my case function BuyStock(obj, src), do
> we have to add one more input parameter to @(src, evnt) to be @(obj, src,
> evnt)?

ADDLISTENER requires the listener function accept exactly two inputs when
specified as a function handle.

If you want to specify a listener that requires a different number of
inputs, or inputs with which the listener function is not called when the
event for which it's listening occurs, you will need to do something like
using an anonymous function to serve as an "adapter" between the syntax
ADDLISTENER expects the listener function to have and the syntax the
function you're calling actually has. As a simpler example, FZERO expects
its function (first) input to be a function of one variable.

r = fzero(@sin, 1)

If you wanted to solve for the root of a Bessel function, which requires two
parameters, but wanted to fix the order this would NOT work:

r = fzero(@besselj, 3) % Not enough input argument

To "adapt" between what FZERO expects and what BESSELJ expects, use an
anonymous function:

f = fzero(@(z) besselj(2, z), 3)

"@(z) besselj(2, z)" is a function that accepts one input (as FZERO
requires) and calls BESSELJ with two inputs (as it requires.)

You'd need to write a similar adapter that accepts the inputs with which
your listener function will be called (src and evnt) and calls your BuyStock
function with the inputs it requires. This documentation page provides a
summary of what I wrote that's more specific to ADDLISTENER:

http://www.mathworks.com/help/techdoc/matlab_oop/brb6gnc.html#brc8nnr

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

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.

rssFeed for this Thread

Contact us