Michal Kutil

Tab Completion for a Matlab Class in the Command Window

Technology:
Matlab - OOP
Tested:
Windows MATLAB Version 7.4.0.287 (R2007a)
Keywords:
Matlab, OOP, user defined class

Tab completion is very fabulous extension of Matlab environment. You can use it in both Command window and Editor. After first few letter of the variable or function writing you can press tab key and pop-up window will show you hints for completion of command. There is a very nice post about this topic: Tab Completion will save your fingers

Here is describing how to modify items inside pop-up hints if you use tab completion for user defined class.

We will use user defined class myclass. This class includes data1 and data2 fields. Myclass has implemented constructor and two methods fcea.m and fceb.m. If you create instance C of this class and uses tab completion you will see following pop-up.

tab completion for user defined class

Data fields of object modification

If we would like modify list of data fields of class in tab-completion we must implement method fieldnames, which will be return list of names available data fields in a cell array. Our method will be return data1 and data3. Data3 is a new virtual data field which doesn't exists in real class. Data2 will not be available for public use. Example of call:

>> fieldnames(C)

ans = 

    'data1'
    'data3'

Now if we press tab for completion of variables and methods instance C we obtain following result:

tab completion with overloaded fieldnames class method

We can see that the data field names were modified exactly in conformity of method fieldnames.

Methods of object modification

If you looked to the previous image, you can see a several methods which myclass includes. If you would like modify this list it is no standards available.

There exists a function method, which returns all methods for given object. You can call this method in a two way. Firstly call with parameter containing name of class and secondly with instance of object. If we implement this method for our class we can overloaded it. In our case we implemented this method to return only following methods name.

>> methods(C)

ans = 

    'fcea'
    'fcec'

But now if press tab for completion variables and methods instance C we obtain the same result as previously (the method methods was added). The main different is in how the Matlab environment call methods fieldnames and methods. The method fieldnames is call in this form fieldnames(C), nevertheless method methods in this form methods('myclass'). In this case of call the method methods is not overloaded by our function and then we obtain following result:

>> m = methods('myclass')

m = 

    'fcea'
    'fceb'
    'fieldnames'
    'methods'
    'myclass'

From this point of view there is no way how to modify list of methods in tab completion. I consult it with people from Mathworks. Here is my question and answer from Ken, cited from: Tab Completion will save your fingers - Comments

Solution

There exists one solution. You can redefine builtin function methods. My methods function is:

function names = methods(this, varargin)
%  Information on class methods

%  Author:  Michal Kutil www.tim.cz
%  Revision: 1.0, Date: 2008-01-23


% varargin prepare
if nargin == 2
    vargin_char = ', varargin{1}';
else
    vargin_char = '';
end

% char to class conversion
if ischar(this)
    try
        eval(['classinstance = ' this '();']);
        if ismethod(classinstance,'methods')
            % call overloaded methods method
            if nargout == 1
                eval(['names = methods(classinstance' vargin_char ');']);
            else
                eval(['methods(classinstance' vargin_char ')']);
            end
            return;
        end
    catch
    end
end

% call builtin function
if nargout == 1
    eval(['names = builtin(''methods'',this' vargin_char ');']);
else
    eval(['builtin(''methods'',this' vargin_char ')']);
end

This function must be place in actual folder or in folder where the path is set. Don't place this function into the class directory i.e. @myclass.

Download: icomethods.m icomyclass

Note 1: don't forget implement method ismethod for your class, which return true for ismethods(C,'methods') or return name methods in array returned in the methods(C) call.

Note 2: Matlab after run or after folder change, displays warning which inform about name conflict with builtin name function. This warning can be suppressed by the command: warning('off', 'MATLAB:dispatcher:nameConflict')

Note 3: this post doesn't describe implementation of some methods which enables to use virtual data fields or vitual methods and on the other side which disables to use suppressed ones. This everything is only about items in tab completion list.

Note 4: During March, April 2008 there will be available new version of Matlab which allow control more information about class. Probably there will be a clean way how to solve depicted problem.

Warning: this solution isn't tested for various toolbox and classes in Matlab. Don't use it for daily work it is only study. You use it completely at your own risk.

If you know how to modify the list of class methods in tab completion without redefine building method methods, or have you got any commentary, or you don't understand something, please send me email to kutilm@fel.cvut.cz.

23. January 2008
Michal Kutil ©2000 - 2012