|
"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
|