0

I have an issue using MATLAB listeners: PreGet and PostGet. My purpose is to be able to execute a set of functions when I am writing and when I am writing a calls property (before and after each read/write operation). This is an example of my class:

classdef myClass < handle 
    properties (GetObservable,SetObservable,GetAccess = public, SetAccess = public )
        myProp = '';
    end

    methods
        function myClass = myClass(Input)

            myClass.myProp = Input;

            addlistener(myClass , 'myProp' , 'PreGet' , @myClass.PreReadClass);
            addlistener(myClass , 'myProp' , 'PreSet' , @myClass.PreSendClass);
            addlistener(myClass , 'myProp' , 'PostGet' , @myClass.PostReadClass);
            addlistener(myClass , 'myProp' , 'PostSet' , @myClass.PostSendClass);
        end
    end

   methods (Hidden)

        function PreSendClass(myClass,  varargin)
            disp('PreSet Executed')
        end
        function PostSendClass(myClass,   varargin)
           disp('PostSet Executed')
        end

        function PreReadClass(myClass,   varargin)
            disp('PreGet Executed')
        end
        function PostReadClass(myClass,   varargin)
           disp('PostGet Executed')
        end
    end

end

MATLAB Run examples:

>> myTest = myClass(1);
myTest.myProp = 1;
PreSet Executed
PostSet Executed

>> myVar = myTest.myProp;
myVar
PreGet Executed
PostGet Executed

myVar =

     1

Everything fine with this. I can execute my functions and get the results which I want. Though, I observed, that wen I use 'Tab Completion' feature from MATLAB on myTest.myProp, PreGet and PostGet is executed, 4 times!! Sometimes myProp is a dynamic structure and I am using Tab completion to access its fields.

myTest.myProp. % Use tab completion
PreGet Executed
PostGet Executed
PreGet Executed
PostGet Executed
PreGet Executed
PostGet Executed
PreGet Executed
PostGet Executed

Is it possible to prevent this from running? Or if not to prevent it at least to detect it? I can run another function when tab completion is executed (e.g. display help or tooltip in command window).

chappjc
  • 30,359
  • 6
  • 75
  • 132
uhnucross
  • 103
  • 1
  • 8
  • Why would you want to prevent this happening? If you're using tab completion to "access the fields" of `myProp`, then you're using tab completion to get the value of `myProp`. So why wouldn't you want the `get` listeners to execute? – Sam Roberts Oct 09 '13 at 10:41
  • Hello. The issue is that the listeners are executed 4 times. – uhnucross Oct 09 '13 at 10:44
  • I also find it surprising that tab completion needs to get the property four times in order to do its job, but it looks like that's what it does. I'm pretty sure there's no way to change tab completion from working the way it does. – Sam Roberts Oct 09 '13 at 11:22
  • Hello @uhnucross . i also found that if you keep pressing the tab button, matlab displays a lot of (java) errors. However i would try to code set and get methods instead of listeners callbacks. Do you _really_ need listeners? – Eugenio Oct 09 '13 at 18:08
  • Hello. Yes I need that listeners. Bot 'Pre' and 'Post'. I am trying now to see if there is away to detect, in the 'Pre' function if I am calling a function with a tab complete or no. – uhnucross Oct 10 '13 at 08:28

0 Answers0