Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
OOP: calling one method from another method in the same class

Subject: OOP: calling one method from another method in the same class

From: Ben Ruppel

Date: 30 Jul, 2012 22:20:42

Message: 1 of 4

Hello,
I have a question related to what is possible in Matlab OOP.

I have the following class:

classdef ClassA
...
methods
    function obj = foo1(obj,A)
         X = ClassA.foo2(A)
    end

    function C = foo2(B)
          B=.......
    end
end
end

For some reason I am getting an error message that "The class ClassA has no property or method named 'foo2'. If I put foo2 outside of the classdef block and change the calling line to "X=foo2(A)" then it works. But it seems to me that a method inside of an object should be able to call another method inside that object, or else why would we have the "private" attribute that we can add to methods? Can anyone tell me if the above code /should/ work?

Thanks!

Subject: OOP: calling one method from another method in the same class

From: Ben Ruppel

Date: 30 Jul, 2012 22:36:52

Message: 2 of 4

Well I was wondering why one would bother to define static methods. Putting foo2 into a Static=true block worked.

From the matlab OOP manual:
Ordinary methods are functions that act on one or more objects and
return some new object or some computed value. These methods are
like ordinary MATLAB functions that cannot modify input arguments.
Ordinary methods enable classes to implement arithmetic operators and computational functions. These methods require an object of the class on which to operate.

I was not working on an object of the class in foo2.

"Ben Ruppel" wrote in message <jv71bq$27n$1@newscl01ah.mathworks.com>...
> Hello,
> I have a question related to what is possible in Matlab OOP.
>
> I have the following class:
>
> classdef ClassA
> ...
> methods
> function obj = foo1(obj,A)
> X = ClassA.foo2(A)
> end
>
> function C = foo2(B)
> B=.......
> end
> end
> end
>
> For some reason I am getting an error message that "The class ClassA has no property or method named 'foo2'. If I put foo2 outside of the classdef block and change the calling line to "X=foo2(A)" then it works. But it seems to me that a method inside of an object should be able to call another method inside that object, or else why would we have the "private" attribute that we can add to methods? Can anyone tell me if the above code /should/ work?
>
> Thanks!

Subject: OOP: calling one method from another method in the same class

From: per isakson

Date: 8 Aug, 2012 05:37:17

Message: 3 of 4

"Ben Ruppel" wrote in message <jv71bq$27n$1@newscl01ah.mathworks.com>...
> Hello,
> I have a question related to what is possible in Matlab OOP.
>
> I have the following class:
>
> classdef ClassA
> ...
> methods
> function obj = foo1(obj,A)
> X = ClassA.foo2(A)
> end
>
> function C = foo2(B)
> B=.......
> end
> end
> end
>
> For some reason I am getting an error message that "The class ClassA has no property or method named 'foo2'. If I put foo2 outside of the classdef block and change the calling line to "X=foo2(A)" then it works. But it seems to me that a method inside of an object should be able to call another method inside that object, or else why would we have the "private" attribute that we can add to methods? Can anyone tell me if the above code /should/ work?
>
> Thanks!

I think this will work as you anticipate

classdef ClassA
...
methods
    function obj = foo1(obj,A)
         X = obj.foo2(A)
    end
    function C = foo2(obj,B)
          C = ....... B
    end
end
end

Subject: OOP: calling one method from another method in the same class

From: Steven_Lord

Date: 8 Aug, 2012 14:01:49

Message: 4 of 4



"per isakson" <poi@bim.kth.se> wrote in message
news:jvstud$fd5$1@newscl01ah.mathworks.com...
> "Ben Ruppel" wrote in message <jv71bq$27n$1@newscl01ah.mathworks.com>...
>> Hello,
>> I have a question related to what is possible in Matlab OOP.
>>
>> I have the following class:
>>
>> classdef ClassA
>> ...
>> methods
>> function obj = foo1(obj,A)
>> X = ClassA.foo2(A)
>> end
>>
>> function C = foo2(B)
>> B=.......
>> end
>> end
>> end
>>
>> For some reason I am getting an error message that "The class ClassA has
>> no property or method named 'foo2'. If I put foo2 outside of the
>> classdef block and change the calling line to "X=foo2(A)" then it works.
>> But it seems to me that a method inside of an object should be able to
>> call another method inside that object, or else why would we have the
>> "private" attribute that we can add to methods? Can anyone tell me if
>> the above code /should/ work?
>>
>> Thanks!
>
> I think this will work as you anticipate
>
> classdef ClassA
> ...
> methods
> function obj = foo1(obj,A)
> X = obj.foo2(A)
> end
> function C = foo2(obj,B)
> C = ....... B
> end
> end
> end

That's one approach; two others are to make foo2 a static method:

http://www.mathworks.com/help/techdoc/matlab_oop/brdqiu3.html

classdef ClassA
    methods
        function obj = foo1(obj,A)
            X = ClassA.foo2(A);
            fprintf('X is %g\n', X);
        end
    end

    methods(Static)
        function C = foo2(B)
            C = 17+B;
        end
    end
end

or, if foo2 doesn't need to know anything about ClassA, it doesn't have to
be a method of ClassA at all:

http://www.mathworks.com/help/techdoc/matlab_oop/brqy3km-14.html#br2la89

% begin ClassA.m
classdef ClassA
    methods
        function obj = foo1(obj,A)
            X = foo2(A);
            fprintf('X is %g\n', X);
        end
    end
end

function C = foo2(B)
    C = 17+B;
end
% end ClassA.m

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us