Reputation: 3524
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??
Upvotes: 7
Views: 9068
Reputation: 1115
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.
Upvotes: 0
Reputation: 43
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
Upvotes: 1
Reputation: 263
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.
Upvotes: 0
Reputation: 3524
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.
Upvotes: 24