Search code examples
matlabevent-listenerbuilt-in

MATLAB: Problem calling builtin('addlistener', ...)


In a Matlab (2017b) class I'm trying to overload addlistener and have problems calling it using the builtin function.

I first thought the problem was caused by the class implementation, but it turns out that I'm able to reproduce the problem with a few lines of code:

% This works ...
l = line(rand(2,1),rand(2,1));
addlistener(l,'Color','PostSet',@(h,e)disp(123));  % create listener
l.Color = rand(1,3);  % trigger listener, '123' is displayed

% This does not ...
builtin('addlistener',l,'Color','PostSet',@(h,e)disp(456)); % ERROR, but why !?

Using the builtin command in the last code line throws the following error:

First argument provided is not valid for addlistener. (Check its type or
validity)

I think the syntax for builtin is correct and would think that this might be a Matlab bug, but maybe someone can prove me wrong :-)


Solution

  • The builtin function addlistener does not support objects of type line, you need the version that is overloaded for line. But you cannot call that version with builtin.

    You likely are trying to call the superclass version of a method. To do so, follow the directions in the manual:

    addlistener@superclass(args)