MATLAB:输入足够的参数,仍然得到一个"没有足够的输入参数"错误

时间:2016-02-11 15:53:09

标签: matlab constructor arguments

我的CoaxLine课程的类构造函数出现问题。我传递了它需要的所有参数,但是当我在另一个程序中创建一个对象时,我得到错误:

Error using length Not enough input arguments.

Error in CoaxLine (line 23) function obj = CoaxLine(pow,len,h,freq,x1,x2,y1,y2,dir,split)

Error in Test2 (line 38) coax1 = CoaxLine(3.9,100,4.75,1800,10,110,10,10,0,1);

即使我删除了构造函数的所有参数要求,并且创建了没有输入的对象,我也得到了相同的长度错误。这是我第一次在MATLAB中构建一个类,所以很可能我错过了一些愚蠢的东西。我很感激帮助。

以下是CoaxLine的代码:

classdef CoaxLine
    %UNTITLED2 Summary of this class goes here
    %   Detailed explanation goes here

    properties
        %Default values
        PA = 3.9;
        orientation = 0; %0 for East-West, 1 for North-South
        splitter = 1; %0 for left side, 1 for right side
        length = 90;
        frequency = 1800; %in MHz
        height = 4.75;
        Ce = 8.77; %Hardcoded for now
        Lint = .13; %Hardcoded
        nearFieldLength = 2*(length^2)/((3.0*10^8)/(frequency*10^6));
        X1 = 10; %Will be points in the simulation axis
        X2 = 110;
        Y1 = 10;
        Y2 = 10;
        %loss = 10;
    end

    methods
        function obj = CoaxLine(pow,len,h,freq,x1,x2,y1,y2,dir,split)
            %if nargin > 0
            obj.PA = pow;
            obj.length = len;
            obj.height = h;
            obj.frequency = freq;
            obj.X1 = x1;
            obj.X2 = x2;
            obj.Y1 = y1;
            obj.Y2 = y2;
            obj.orientation = dir;
            obj.splitter = split;
            %end
        end

        function r = contribution(px,py)
            if(obj.orientation == 0)
                if(obj.splitter)
                    if(abs(py - obj.Y1) <= obj.nearFieldLength && px > obj.X1 && px < obj.X2)
                        H = abs(py - obj.Y1);
                        x = px - obj.X1;
                        r = NearFieldPropagation(obj.PA,obj.length,obj.frequency,H,obj.height,obj.Ce,obj.Lint,x);
                    end
                else
                    if(abs(py - obj.Y1) <= obj.nearFieldLength && px < obj.X1 && px > obj.X2)
                        H = abs(py - obj.Y1);
                        x = obj.X1 - px;
                        r = NearFieldPropagation(obj.PA,obj.length,obj.frequency,H,obj.height,obj.Ce,obj.Lint,x);
                    end
                end

                %else

            end
        end
    end
end

1 个答案:

答案 0 :(得分:0)

错误源于这一行:

    nearFieldLength = 2*(length^2)/((3.0*10^8)/(frequency*10^6));

MATLAB认为您正在尝试调用函数length。这需要一个参数,其长度将被返回。

使用frequency也会让您头疼。要正确处理此类properties,您应该将nearFieldLength声明为Dependenthttp://www.mathworks.com/help/matlab/matlab_oop/access-methods-for-dependent-properties.html,然后为其编写一个可以即时计算其值的getter。

此外,正如excaza所指出的那样,您还有其他错误,因为您未在obj中声明contribution作为参数。

这是关于代码应该如何的想法:

classdef CoaxLine

        properties
                PA          = 3.9;
                orientation = 0;
                splitter    = 1;
                length      = 90;
                frequency   = 1800;
                height      = 4.75;
                Lint        = .13;
                X1 = 10;
                X2 = 110;
                Y1 = 10;
                Y2 = 10;
        end;

        properties(Dependent, SetAccess=private)
                Ce;
                nearFieldLength;
        end;

        methods
                %//Constructor
                function obj = CoaxLine(pow,len,h,freq,x1,x2,y1,y2,dir,split)
                        if nargin > 0
                                obj.PA          = pow;
                                obj.length      = len;
                                obj.height      = h;
                                obj.frequency   = freq;
                                obj.X1          = x1;
                                obj.X2          = x2;
                                obj.Y1          = y1;
                                obj.Y2          = y2;
                                obj.orientation = dir;
                                obj.splitter    = split;
                        end;
                end;


                %//Getters for dependent properties
                function val = get.Ce(obj)  %#ok<MANU>
                        val = 8.77;  %//this can be changed later
                end;

                function val = get.nearFieldLength(obj)
                        val =  2*(obj.length^2)/(3E8/(obj.frequency*1E6));
                end;


                %//Normal methods
                function r = contribution(obj, px, py)
                        r = []; % some default value
                        if obj.orientation == 0
                                if obj.splitter
                                        if abs(py - obj.Y1) <= obj.nearFieldLength ...
                                        && px > obj.X1 ...
                                        && px < obj.X2
                                                H = abs(py - obj.Y1);
                                                x = px - obj.X1;
                                                r = NearFieldPropagation(obj.PA,obj.length,obj.frequency,H,obj.height,obj.Ce,obj.Lint,x);
                                        end;
                                else
                                        if abs(py - obj.Y1) <= obj.nearFieldLength ...
                                        && px < obj.X1 ...
                                        && px > obj.X2
                                                H = abs(py - obj.Y1);
                                                x = px - obj.X1;
                                                r = NearFieldPropagation(obj.PA,obj.length,obj.frequency,H,obj.height,obj.Ce,obj.Lint,x);
                                        end;
                                end;
                        end;
                end;
        end;
end