matlabclassoopmatlab-class

Call class method from within class: not enough input arguments


Let's define the following class with some nested methods:

classdef MyClass
    methods(Static)
        function a = simple_func(obj)
            a = obj.simple_func2(1);
        end
        function b = simple_func2(obj,arg1)
            b = arg1+1;
        end
    end
end

And call a class method as follow:

c = MyClass
a = c.simple_func()

I get the following error:

Not enough input arguments.

Error in MyClass.simple_func (line 4)
        a = obj.simple_func2(1);

Error in main (line 2)
a = c.simple_func();

What is the issue with my class definition? I read so much Matlab documentation that I'm lost now. I use MATLAB 2023b.


Solution

  • You've defined the methods as Static, which means that c.simple_func() doesn't call simple_func(c), but just MyClass.simple_func(). If you remove the Static declaration, it'll work as intended.