سؤال

I have the following class in matlab:

classdef floating_search
properties
    S,M;
end
methods
    function s = support(x,y)
        for i=1:length(x)
            if(y(i)~=1)
                s = x(i);
            end
        end
    end
end
end

Now, in the command winows, I did the following:

>> x=1:10;
>> floating_search.S = x;
>> y=trapmf(x,[1 3 5 9])

y =

  Columns 1 through 7

         0    0.5000    1.0000    1.0000    1.0000    0.7500    0.5000

  Columns 8 through 10

    0.2500         0         0

>> floating_search.M = y; 
>> floating_search.support(floating_search.S, floating_search.M)
??? Reference to non-existent field 'support'.

For the last command, why did I get this error? Am I calling the function wrong? How can I pass th values floating_search.S and floating_search.M to the function and retrieve the values of S for which Y~=1?

Thanks.

هل كانت مفيدة؟

المحلول 2

your class is missing a constructor. furthermore you never initialize any object.

your floating_search.S = x; statement generates a struct called floating_search:

>> whos floating_search
  Name                 Size            Bytes  Class     Attributes

  floating_search      1x1               256  struct  

Try this instead (save file as floating_search.m):

classdef floating_search
    properties
        S;
        M;
    end

    methods

        % constructor - place to initialize things
        function obj = floating_search()
        end        

        % you need the first input argument 'obj', since this is a value class
        % see http://www.mathworks.de/de/help/matlab/matlab_oop/comparing-handle-and-value-classes.html
        function s = support(obj, x, y)
            for i=1:length(x)
                if(y(i)~=1)
                    s = x(i);
                end
            end
        end

    end
end

and then run the code:

% generate your data
x = 1:10; 
y = trapmf(x,[1 3 5 9]);

# initialize object
a = floating_search()
a.S = x;
a.M = y;
a.support(a.S, a.M)

نصائح أخرى

You never initialize your object. Furthermore, I think you should reconcider using your code as a non-static method:

classdef floating_search

properties

   S
   M

end

methods
    function s = support(obj)
        for i=1:length(obj.S)
            if(obj.M(i)~=1)
                s = obj.S(i);

            end
        end
    end
end

end

Then execute:

x = 1:10;
y = trapmf(x,[1 3 5 9])

myInstance = floating_search()
myInstance.S = x;
myInstance.M = y; 

myInstance.support()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top