5

Assume I have an object X of class MyClass. MyClass has a method compute, and when I call U = compute(X,...), matlab automatically calls the class method. However, what I actually want is to call another function also called compute whose parameters start with a MyClass object though. How do I force matlab to call this regular function rather than go into the class method?

Amro
  • 123,847
  • 25
  • 243
  • 454
OneZero
  • 11,556
  • 15
  • 55
  • 92
  • 1
    any reason why both versions are not member methods? you could also make the external function a static method of the class, that way it is called differently: `MyClass.compute(x)` vs. `compute(x)` – Amro Jul 10 '13 at 23:06

3 Answers3

6

There is no way to do this without making some changes either to the function's name or location. If you check Matlab's function precedence order, methods always run before normal external functions. Your only practical options are:

  1. Change the function's name.
  2. Move the function's body to the same script that is calling the function (item 4 on the list above)
  3. Move the function's .m file to a folder called private in the same folder as your script file (item 5 on the list)

UPDATE

Although not quite practical for smaller projects, you may also want to look into packaging your functions. A good discussion can be found in this SO post.

Community
  • 1
  • 1
Bee
  • 2,472
  • 20
  • 26
  • The problem is, even I change the function name to `compute2`, matlab still tries to call the class method and winded up reporting an error since `compute2` is not defined in `MyClass`... – OneZero Jul 10 '13 at 20:50
  • actually if you renamed the regular function as `compute2` it will be called correctly (I just tried it in R2013a) – Amro Jul 10 '13 at 23:10
  • @OneZero Remember that you need to change both the function name and the filename containing that function. So for example you will end up with `function x = compute2(obj)` inside the file `compute2.m`. – Bee Jul 11 '13 at 12:58
2

If your compute happens to be a MATLAB builtin, you can use

builtin('compute', ...)

otherwise, there's no way -- see Bee's answer.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Good point. I just assumed since his `compute` function accepts the custom object as the first parameter, it's not a built-in function. – Bee Jul 10 '13 at 20:13
  • 1
    @Bee: true. In all likelihood the OP isn't dealing with a builtin. But someone else might :p – Rody Oldenhuis Jul 10 '13 at 20:13
2

If you desperately need this, then you can do something like the following. I strongly suggest that you don't, and stick with Bee's answer. However, sometimes one has no choice ...

The idea is to wrap your instance in another class so that MATLAB's function dispatching doesn't see the compute method. However, to your compute function, the wrapped instance must appear the same as the original instance. This is tricky to get right in some cases, but often the following is enough:

classdef Wrapper

    properties (Access = 'private', Hidden = true)
        core = [];
    end

    methods

        function this = Wrapper(core)
            this.core = core;
        end

        function varargout = subsref(this, S)
            if nargout > 0
                varargout = cell(1, nargout);
                [varargout{:}] = subsref(this.core, S);
            else
                subsref(this.core, S);
            end
        end

    end

end

This class wraps an instance of another class and delegates all read access to the wrapped instance.

For example, if you have a file called TestClass.m:

classdef TestClass

    properties
        name = '';
    end

    methods
        function this = TestClass(name)
            this.name = name;
        end

        function compute(this)
            fprintf('Instance method! My name is "%s".\n', this.name);
        end
    end

end

And a function compute.m:

function compute(x)
    fprintf('Regular function! My name is "%s".\n', x.name);
end

Then it works like this:

>> t = TestClass('t');
>> s = struct('name', 's');
>> compute(t)
Instance method! My name is "t".
>> compute(s)
Regular function! My name is "s".
>> w = Wrapper(t);
>> compute(w)
Regular function! My name is "t".

Depending on what the compute function does with your instance you might need to add further "special" functions to Wrapper (e.g. subsasgn). Also note that this will break if compute does some metaclass-magic.

Florian Brucker
  • 9,621
  • 3
  • 48
  • 81