TSL_
TSL_

Reputation: 2079

write MATLAB class in separate files

It's nothing new to ask such question. Since I've spent some hours reading on the documentation and available examples. But awkwardly, MATLAB seems not accept my code. So, I feel totally confused

classdef testclass

    %UNTITLED Summary of this class goes here
    %   Detailed explanation goes here

    properties
        a =3;
        b =4;
    end

    methods

        function obj = testclass(a_inp, b_inp) % 
            obj.a = a_inp;
            obj.b = b_inp;
        end

        output = plus(obj)

        output = minus(obj)
    end

end

and the function

function output = minus(obj)

output = obj.a-obj.b;
end

I think this is a typical test. But when I initialize the class, it comes this error

Error using testclass
Too many input arguments.

Error in class_call (line 2)
myclass = testclass(3,4    );

I create the class by myclass = testclass(3,4 );

what might be wrong in my code? plz help! I'm stuck!

Upvotes: 0

Views: 552

Answers (1)

Navan
Navan

Reputation: 4477

The code you have is correct. As @ horchler says only thing you need is to put all your files including class definition inside @testclass directory. Without this directory you cannot have separate files for a class.

The code you tried for instantiating the class also should work fine outside @testclass directory. You would get error only when calling the method plus or minus if you have not placed your files in @testclass directory.

Upvotes: 1

Related Questions