Thread Subject: matlab class syntax

Subject: matlab class syntax

From: Chang shen

Date: 12 Jan, 2009 21:03:01

Message: 1 of 3

Hi all,

I want to test a syntax of matlab classdef. In the following dummy class. All the methods are for testing syntax only. No real purpose. The static methods work but the regular methods do not work, but m-file methdos work again Why?

There are 3 kind methods. They are almost identical, I use MyMethod(), MyMethod1() and MyMethod2() naming them.

(1)static methods, call with classX.MyMethod();
(2)public method, call with obj.MyMethod2();
(3)m-file methods, call with MyMethod1();

I marked lines that get the error.

classdef classX
    properties
       data;
       meanv=0;
    end
    methods %constractor
      function obj = classX(datav)
          obj.data = datav;
          obj.meanv = classX.Getmean(obj); %works, static
          obj.meanv = Getmean1(obj); %works, m-file
          obj.meanv = obj.Getmean2(); %error, due to GetLineMean2, it is the
                                                              %same as GetLineMean1 in m-file way.
         
      end
    end
    
    methods (Access = public)
         function proj1 = Getproj2(obj)
            proj1 = mean(obj.data,1);
        end
        function meanv1 = Getmean2(obj)
            line = obj.Getproj2();
            meanv1 = obj.GetLineMean2(line); %%%%%error
        end
        function linemean = GetLineMean2(line)
            linemean = mean(line);
        end
        
    end
    methods (Static = true)
        function proj1 = Getproj(obj)
            proj1 = mean(obj.data,1);
        end
        function meanv1 = Getmean(obj)
            line = classX.Getproj(obj);
            meanv1 = classX.GetLineMean(line);
        end
        function linemean = GetLineMean(line)
            linemean = mean(line);
        end
    end
    
end



%%%%%%%%%%%%%%%%%%%%%%%%
%those are m-files saved in matlab path
%%%%%%%%%%%%%%%%%%%%%%%%

%m-file: Getproj1
function proj1 = Getproj1(obj)
            proj1 = mean(obj.data,1);
end

%m-file: Getmean1
function meanv1 = Getmean1(obj)
            line = Getproj1(obj);
            meanv1 = GetLineMean1(line);
end

%m-file: GetLineMean
function linemean = GetLineMean1(line)
            linemean = mean(line);
end

%%%%%%%%%%%%%%%%%%%%%%%

Thanks
Chang

Subject: matlab class syntax

From: Steven Lord

Date: 12 Jan, 2009 21:41:20

Message: 2 of 3


"Chang shen" <shencs@yahoo.com> wrote in message
news:gkgb65$e21$1@fred.mathworks.com...
> Hi all,
>
> I want to test a syntax of matlab classdef. In the following dummy class.
> All the methods are for testing syntax only. No real purpose. The static
> methods work but the regular methods do not work, but m-file methdos work
> again Why?
>
> There are 3 kind methods. They are almost identical, I use MyMethod(),
> MyMethod1() and MyMethod2() naming them.
>
> (1)static methods, call with classX.MyMethod();
> (2)public method, call with obj.MyMethod2();
> (3)m-file methods, call with MyMethod1();
>
> I marked lines that get the error.
>
> classdef classX
> properties
> data;
> meanv=0;
> end
> methods %constractor
> function obj = classX(datav)
> obj.data = datav;
> obj.meanv = classX.Getmean(obj); %works, static
> obj.meanv = Getmean1(obj); %works, m-file
> obj.meanv = obj.Getmean2(); %error, due to
> GetLineMean2, it is the
> %same as
> GetLineMean1 in m-file way.
>
> end
> end
>
> methods (Access = public)
> function proj1 = Getproj2(obj)
> proj1 = mean(obj.data,1);
> end
> function meanv1 = Getmean2(obj)
> line = obj.Getproj2();
> meanv1 = obj.GetLineMean2(line); %%%%%error
> end
> function linemean = GetLineMean2(line)
> linemean = mean(line);
> end

*snip the rest of the code*

This problem isn't related to whether the method is static, a separate
M-file, or a subfunction inside the classdef file.

Look at how you're calling GetLineMean2 on the line you've marked. How many
input arguments are you passing into GetLineMean2? If you said one, you're
incorrect. You're passing in _two_ input arguments -- the second of which
is line, and the first of which is the object itself. However, GetLineMean2
is only expecting one input argument. To correct this problem, change the
declaration of GetLineMean2 to:

function linemean = GetLineMean2(obj, line)

and it should work. Alternately, since GetLineMean2 doesn't actually do
anything on the object, that's an indication that you may want to make it a
normal subfunction inside this class definition, not a method. To do that,
change the line in the Getmean2 method to:

meanv1 = GetLineMean2(line);

and move GetLineMean2 after the END corresponding to the CLASSDEF line in
the classX.m file. If you use this approach, do not add the obj input
argument to GetLineMean2. You could, of course, also make it its own
separate M-file (if you wanted to access that function from multiple
functions or methods) and that should work as well.

--
Steve Lord
slord@mathworks.com

Subject: matlab class syntax

From: Chang shen

Date: 13 Jan, 2009 16:48:01

Message: 3 of 3

Great Answer!


"Chang shen" <shencs@yahoo.com> wrote in message <gkgb65$e21$1@fred.mathworks.com>...
> Hi all,
>
> I want to test a syntax of matlab classdef. In the following dummy class. All the methods are for testing syntax only. No real purpose. The static methods work but the regular methods do not work, but m-file methdos work again Why?
>
> There are 3 kind methods. They are almost identical, I use MyMethod(), MyMethod1() and MyMethod2() naming them.
>
> (1)static methods, call with classX.MyMethod();
> (2)public method, call with obj.MyMethod2();
> (3)m-file methods, call with MyMethod1();
>
> I marked lines that get the error.
>
> classdef classX
> properties
> data;
> meanv=0;
> end
> methods %constractor
> function obj = classX(datav)
> obj.data = datav;
> obj.meanv = classX.Getmean(obj); %works, static
> obj.meanv = Getmean1(obj); %works, m-file
> obj.meanv = obj.Getmean2(); %error, due to GetLineMean2, it is the
> %same as GetLineMean1 in m-file way.
>
> end
> end
>
> methods (Access = public)
> function proj1 = Getproj2(obj)
> proj1 = mean(obj.data,1);
> end
> function meanv1 = Getmean2(obj)
> line = obj.Getproj2();
> meanv1 = obj.GetLineMean2(line); %%%%%error
> end
> function linemean = GetLineMean2(line)
> linemean = mean(line);
> end
>
> end
> methods (Static = true)
> function proj1 = Getproj(obj)
> proj1 = mean(obj.data,1);
> end
> function meanv1 = Getmean(obj)
> line = classX.Getproj(obj);
> meanv1 = classX.GetLineMean(line);
> end
> function linemean = GetLineMean(line)
> linemean = mean(line);
> end
> end
>
> end
>
>
>
> %%%%%%%%%%%%%%%%%%%%%%%%
> %those are m-files saved in matlab path
> %%%%%%%%%%%%%%%%%%%%%%%%
>
> %m-file: Getproj1
> function proj1 = Getproj1(obj)
> proj1 = mean(obj.data,1);
> end
>
> %m-file: Getmean1
> function meanv1 = Getmean1(obj)
> line = Getproj1(obj);
> meanv1 = GetLineMean1(line);
> end
>
> %m-file: GetLineMean
> function linemean = GetLineMean1(line)
> linemean = mean(line);
> end
>
> %%%%%%%%%%%%%%%%%%%%%%%
>
> Thanks
> Chang

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
class C Shen 12 Jan, 2009 16:05:03
classdef C Shen 12 Jan, 2009 16:05:03
syntax C Shen 12 Jan, 2009 16:05:03
static method C Shen 12 Jan, 2009 16:05:03
rssFeed for this Thread

Contact us at files@mathworks.com