0
votes

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!

1
AFAIK I know, MATLAB has no concept of partial classes that can be spread across multiple m files. You'll have to define those methods in the same file, within the class definition. - Praetorian
Are you looking for this? @Praetorian isn't correct. Either you define everything in one big file or you define everything based on the @MyClass folder scheme. The lines output = plus(obj) and output = minus(obj) in your classdef make no sense. - horchler
@horchler Thanks! It's been a while since I've looked at the OOP stuff in MATLAB and clearly I have some catching up to do. - Praetorian
thank you for your common interests in MATLAB OOP! @horchler: that's the signatures of the functions contained in separate files and it's mentioned in section "Specifying Method Attributes in classdef File" in your link - TSL_
What is class_call? And why are there extra spaces after your 4? are you changing what the real inputs are? I can't replicate this. Can you call obj = testclass(3,4) from the command window? Also, there's no need to include function signatures if you're not specifying any method attributes like Static or Access=private (I suppose it doesn't hurt but it looks weird). - horchler

1 Answers

1
votes

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.