Matlab Class Method: Too many arguments
Asked Answered
H

4

7

I found some related questions, but didn't really find an answer in there.

I'm writing a simple little MATLAB class in order to learn OOP syntax in MATLAB. I'm very familiar with Python, and pulling my hair out trying to work with MATLAB. Here's the definition:

classdef Car < handle


    properties
        speed = [0,0]   %x,y velocity
        position = [0,0]
        running = false

    end

    methods
        function obj = Car(pos, spd)
            obj.position = pos;
            obj.speed = spd;
        end

        function accelerate(obj,x,y)    % Add to speed
            obj.speed = obj.speed + [x,y]
        end

        function position = getPosition(obj)
            position = obj.position
        end

        function start(obj)
            obj.running = true
        end

        function stop(obj)
            obj.running = false
        end
    end

end

This is certainly not done, but then I'm using a little script to mess with the object:

foo = Car([1,1],[0,2])
foo.start
foo.accelerate(2,3)

Instantiation works, but ANY method I call throws an error. foo.start, for example:

Error using Car/start
Too many input arguments.

What am I missing??

Hoedown answered 8/5, 2013 at 17:22 Comment(3)
I should clarify that it is always the same "Too many input arguments" error for any method I try to call on an object.Hoedown
What version of MATLAB? Your code works for me on R2011b.Kephart
sh*t... I just used the clear command, and now it's working. Should I delete this question?Hoedown
H
24

Since I can't figure out how to delete this question, I'll do my best to answer it. Like other languages, object oriented programing in MATLAB wants to see obj as the first parameter in class methods (like self in python). This reference to the object is necessary to modify its attributes. I was not including this in method definitions, so when I called the method I was getting the "too many arguments" error. That's because if you do foo.method(a,b), then the object foo is actually passed as a parameter, so you function is actually getting 3 inputs, i.e. method(foo,a,b).

I went through my code and added obj in the appropriate places, but FAILED TO USE THE clear COMMAND in the MATLAB command window. Since I'm new to MATLAB, I was unaware of its importance. I just assumed saving the file and re-instantiating my class would be sufficient. It is not.

I hope this helps anybody who comes across this question.

Hoedown answered 8/5, 2013 at 17:48 Comment(0)
L
1

You don't need to pass the obj to the input if you declare the methods as static:

classdef class1
    methods (Static)
        function y=aPLUSb(a,b)
            y=a+b;
        end
    end
end
Lindbergh answered 2/8, 2015 at 17:19 Comment(0)
A
0

You answered your own question which is great. But, it seems that your question includes the very code that you struggled to executed in the beginning. I would have thought that your code in the beginning looked like:

classdef Car < handle


    properties
        speed = [0,0]   %x,y velocity
        position = [0,0]
        running = false

    end

    methods
        function obj = Car(pos, spd)
            obj.position = pos;
            obj.speed = spd;
        end

        function accelerate(x,y)    % Add to speed
            obj.speed = obj.speed + [x,y]
        end

        function position = getPosition()
            position = obj.position
        end

        function start()
            obj.running = true
        end

        function stop()
            obj.running = false
        end
    end

end

and, only later, became what you wrote in your question:

classdef Car < handle


    properties
        speed = [0,0]   %x,y velocity
        position = [0,0]
        running = false

    end

    methods
        function obj = Car(obj,pos, spd)
            obj.position = pos;
            obj.speed = spd;
        end

        function accelerate(obj,x,y)    % Add to speed
            obj.speed = obj.speed + [x,y]
        end

        function position = getPosition(obj)
            position = obj.position
        end

        function start(obj)
            obj.running = true
        end

        function stop(obj)
            obj.running = false
        end
    end

end

Note that executing the code in your question allows me to access and execute all methods declared in your class. Trying to access methods declared in the first code block would result in the "too many arguments" error. Trying to access methods declared in the second block works just as expected.

Advancement answered 24/2, 2015 at 23:17 Comment(0)
S
0

I suspect that not many people would encounter my cause behind such a message, but it was because I use an external legacy text editor to write code. And I did not save the updated class definition *.m file that specifies the method prototype (which has a corresponding *.m file for that method alone).

It's one of those causes that makes me smack my forehead. Yes, legacy power text editors have their advantages, but integration with the development environment isn't one of them. Hours and hours of intrigue.

Strachey answered 10/11, 2022 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.