How to force matlab to call a regular function rather than class method when they are overloaded?
Asked Answered
Y

3

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?

Yenta answered 10/7, 2013 at 16:54 Comment(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)Lundgren
M
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.

Monetary answered 10/7, 2013 at 18:21 Comment(3)
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...Yenta
actually if you renamed the regular function as compute2 it will be called correctly (I just tried it in R2013a)Lundgren
@Yenta 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.Monetary
Z
2

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

builtin('compute', ...)

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

Zerline answered 10/7, 2013 at 19:54 Comment(2)
Good point. I just assumed since his compute function accepts the custom object as the first parameter, it's not a built-in function.Monetary
@Bee: true. In all likelihood the OP isn't dealing with a builtin. But someone else might :pZerline
Z
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.

Zephan answered 11/7, 2013 at 6:54 Comment(2)
that last doStuff(w) should be compute(w). +1 for the workaround, but I too do not recommend using this in real code :)Lundgren
@Amro: Thanks, fixed. That was leftover from my trial-and-error code.Zephan

© 2022 - 2024 — McMap. All rights reserved.