WG-
WG-

Reputation: 1070

Script cannot find/call function in matlab

I have some code in a folder which contains some basic functions regarding fuzzy sets. These folders are @fset and @mftrap. I can call the functions from those folders without any problem.

Now I defined a new function, tsmodel.m. I putted this first in the root i.e.

/root
 + /@fset
 + /@mftrap
 - tsmodel.m
 - example.m

I could call tsmodel.m from example.m. Now I putted it in @fset folder since it is related. But now my example.m file cannot find tsmodel.m anymore for some reason while I can still call all other functions located in @fset folder. I get the following error:

Undefined function 'tsmodel' for input arguments of type
'cell'.

Error in example (line 21)
Y = tsmodel(X,t,a,b);

I believe the problem lies in the fact that the folder @fset is declared as a class or something.

Hopefully anyone can tell me what is going wrong.

-edit-

Sorry I forgot to include the files: https://dl.dropboxusercontent.com/u/20782274/fuzzy.zip

-edit2-

I now changed the @fset folder to a single class file. How can I now set method of tsmodel such that it can accept a cell array of fset methods?

    classdef fset
    properties (SetAccess = private)
        mu
        x
    end

    methods  
        function A = fset(x,m)
        % constructor for a fuzzy set
            if nargin < 2 
                m = ones(size(x)); 
            end

            if isa(m,'fset') 
                A = m; 
                return; 
            end

            if isa(m,'mftrap'), 
               A.mu = mu(x,m); 
            else 
               A.mu = m(:); 
            end

            A.x = x(:);
        end

        function h = plot(A,linetype)
        % plot membership function
            if nargin < 2
                linetype = 'b'; 
            end

            A = A(:);

            hs = ishold;

            for i = 1 : length(A)
               handle(i) = plot(A(i).x,A(i).mu,linetype);
               hold on
            end

            if ~hs
                hold off;
            end

            title(inputname(1))
            ylabel('mu');
            xlabel('x');

            if nargout
                h = handle;
            end
        end

        function display(A)
        % display fuzzy set as a pointwise list
            A  = A(:);
            lA = length(A);

            for i = 1:lA
                if iscell(A(i).x)
                    if lA > 1
                        str = sprintf('%s(%d) = \n\n  %s', ...
                                      inputname(1),i,'fuzzy relation');
                    else   
                        str = sprintf('%s = \n\n  %s', ...
                                      inputname(1),'fuzzy relation');
                    end;

                    disp(str);
                    disp(A(i).mu);
                else   
                    list = [A(i).mu'; A(i).x'];
                    list = list(:);

                    if size(list,1) < 10,
                        s = sprintf('%1.2f/%1.2f, ',list);
                    else
                        list1 = list(1:4,:);
                        list2 = list(end-3:end,:);
                        s = [sprintf('%1.2f/%1.2f, ',list1(:)) '..., ' ...
                             sprintf('%1.2f/%1.2f, ',list2(:))];
                    end;

                    if lA > 1,
                        str = sprintf('%s(%d) = \n\n  fuzzy set\n  { %s }', ...
                                      inputname(1),i,s(1:end-2));
                    else   
                        str = sprintf('%s = \n\n  fuzzy set\n  { %s }', ...
                                      inputname(1),s(1:end-2));
                    end;

                    disp(str);

                end
            end
        end

        function Y = tsmodel(A,X,a,b)
        % TSMODEL
        % input:
        %   X, dataset
        %   A, fuzzy set
        %   a, affine linear function parameter
        %   b, affine linear function parameter
        % output:
        %   Y, output

            % check input
            if isempty(X) || isempty(A) || ...
               isempty(a) || isempty(b)
                error('Not all necesary parameters are given.');
                return;
            end

            % check if singleton model (only b are given)
            if size(a) == 0
                a = zeros(1,length(b));
            end

            % check dimension A and a and trap and b
            if length(A) ~= length(a) || length(A) ~= length(b)
                error('Dimensions do not match');
                return;
            end

            % init storage variables
            Y      = zeros(length(X),1);
            weight = zeros(length(A),1);

            % for each x in X
            for k = 1:length(X)

                % for each rule
                for i = 1:length(A)
                    weight(i) = mu(X(k),A{i});
                end

                % compute weighted mean
                Y(k) = sum(weight .* (a*X(k) + b)) / sum(weight);
            end
        end
    end
end

Upvotes: 1

Views: 248

Answers (1)

Sanjay Manohar
Sanjay Manohar

Reputation: 7026

File in the @fset folder are all "methods" of the class fset. That means, they should all "operate on" fset objects. In fact, they all take an fset as their first parameter.

  • If your method is like that, i.e. it acts upon fset objects, then it does belong in the @fset folder. In this case, you need to edit the file /@fset/fset.m which defines the class, and add it to the methods section. It should then become visible.

  • If your method isn't like that, i.e. it doesn't operate on an fset object, then it doesn't strictly belong in the @fset folder -- even though it's related. That's just the law! you can put it anywhere else.

see here for how to organise the class folders, and here for how to edit the class definition file.

Upvotes: 2

Related Questions