Reputation: 1696
I am very new to programming in Matlab, I just want to try out a small OOP scenario, but it does not quite work as expected. Here's my code:
classdef testOOP
properties
age = 0;
subject = '';
end
methods
function ret = getSubject(this)
ret = this.subject;
end
function ret = getAge(this)
ret = this.age;
end
end
end
When I run the m file and try out the functions in the command prompt, I get the exception "Too many output arguments":
ans =
testOOP
>> x = testOOP
x =
testOOP
>> x.age = 4
x =
testOOP
>> x.age
ans =
4
>> x.getAge
ans =
4
>> x.getAge()
ans =
4
>> y = x.getAge()
??? Error using ==> getAge
Too many output arguments.
I'd like to know why this is failing, even though it works when I use
y = ans
Upvotes: 2
Views: 500
Reputation: 1696
I just found this nice tutorial: http://www.cs.ubc.ca/~murphyk/Software/matlabTutorial/html/objectOriented.html#2
It says there were changes to Matlab's OOP from version 2008b on.
I am using Matlab 2007b (should have mentioned it, sorry...), where the syntax is like this:
>> a = testOOP
a =
testOOP
>> getAge(a)
ans =
0
>> y = getAge(a)
This finally works.
Upvotes: 3
Reputation: 36710
I tried your code (now including the last line) and I don't get any error. There is only one explanation which comes to my mind:
Try clear all
, which should delete all instances, then retry.
Upvotes: 2