RobertG
RobertG

Reputation: 1696

Simple Object-Oriented Programming in Matlab: "Too many output arguments"

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

Answers (2)

RobertG
RobertG

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

Daniel
Daniel

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:

  • You created a broken class
  • An instance was created
  • You fixed the class, but as long as the old version is instanced, Matlab does not update the class definition.

Try clear all, which should delete all instances, then retry.

Upvotes: 2

Related Questions