【问题标题】:Undefined function or method xxx for input arguments of type 'double''double' 类型的输入参数的未定义函数或方法 xxx
【发布时间】:2013-12-11 23:34:50
【问题描述】:

所以我四处寻找导致此错误的原因,人们不断列出罪魁祸首是 matlab 不知道函数的目录路径。我检查了一下,这似乎不是问题,因为 matlab 知道路径。我这里有一些代码假设要进行高斯消除

classdef gaussfunctions
    %UNTITLED Summary of this class goes here
    %   Detailed explanation goes here

properties
end

methods (Static)
    function solution_vec = gauss_reduce(param_mat, const_vec)
        %check for conistent sixe of param_mat and const_vec
        [n,m] = size(param_mat);

        if n ~= m | n ~= size(const_vec)
            disp('Non square matrix or constant vector was incorrect size');
            solution_vec = zeros(size(const_vec));
            return

        end

        %reduce coeffeicient matrix to upper triangular
        [ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec);

        %compute the solution vector
        solution_vec = back_subst(ut_mat, new_const_vec);
    end

    function [ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec)
        %get the size of the parameter matrix
        [m,n] = size(param_mat);

        %get the values of the arguments to change and the return
        cur_mat = param_mat;
        cur_vec = const_vec;

        %loop through all of the columns to reduce them
        for cur_col = 1:m,
            [cur_mat, cur_vec] = reduce_column(cur_mat, cur_vec, cur_col);
        end

        %return the values
        ut_mat = cur_mat;
        new_const_vec = cur_vec;

    end

    function [new_mat, new_const_vec] = reduce_column(param_mat, const_vec, column)
        %get the size 
        [m,n] = size(param_mat);

        %copy the argument values to change them and the return
        cur_mat = param_mat;
        cur_vec = const_vec;

        %check to see if the pivot is zero or not
        if param_mat(column,column) == 0

            disp('Zero pivot encourtered');
            new_mat = param_mat;
            new_const_vec = zeros(size(const_vec));

            return
        end

        %loops down calling reduce_row_at_col on all row below the
        %current column
        for i = column + 1:n,
            [cur_mat, cur_vec] = reduce_row_at_col(cur_mat, cur_vec, column, column, i);
        end

        %return the end values
        new_mat = cur_mat;
        new_const_vec = cur_vec;

    end


    function [new_mat, new_const_vec] = reduce_row_at_col(param_mat, const_vec, col, row_added, row_reduced)
        %transfer over the values
        new_mat = param_mat;
        new_const_vec = const_vec;

        %get the multiple that will be multiplied by the row that will
        %be added
        m = (-param_mat(row_reduced,col))/param_mat(row_added, col);

        %change the value of the new_mat at the row that's going to be reduced 
        new_mat(row_reduced,col) = param_mat(row_reduced,col) + m*param_mat(row_added,col);

        %change the value of the new_const_vec at the row that's going
        %to be reduced
        new_const_vec(row_reduced) = const_vec(row_reduced) + m*const_vec(row_reduced);

    end

    function solution_vec = back_subst(ut_mat, new_const_vec)
        %get the size of the matrix
        [n,m] = size(ut_mat);
        %set the partial soltuions to zeroes
        part_solution_vec = zeros(new_const_vec);

        %start from the last column and work backwards
        for i = n: -1: 1,
            part_solution_vec = back_subst_for_col(ut_mat, new_const_vec, i, part_solution_vec);
        end

        %return the final answer
        solution_vec = part_solution_vec;

    end

    function new_part_solution_vec = back_subst_for_col(ut_mat, new_const_vec, column, part_solution_vec)
        %get size
        [n,m] = size(ut_mat);
        %value used for calculation
        subst_val;
        %copy over the partial solution
        new_part_solution_vec = part_solution_vec;

        %if we are at the last element then we want to do a simplified
        %calculation
        if column == n
            new_part_solution_vec(column) = new_const_vec(column)/ut_mat(column,column);
            return
        end

        %otherwise calculate the soltuion through back substituion
        for i = column + 1: n,
            subst_val = subst_val + ut_mat(column,i)*part_solution_vec(i);
        end

        %do the final calculation
        new_part_solution_vec(column) = (new_const_vec(column) - subst_val)/ut_mat(column,column);

    end

end

结束

所以问题是一个函数可以被调用,但是当它调用一个辅助函数时会报错。这是我在命令窗口中调用gauss_reduce()的示例

gaussfunctions.gauss_reduce(A,B)
??? Undefined function or method 'ut_reduce' for input arguments of type 'double'.

Error in ==> gaussfunctions>gaussfunctions.gauss_reduce at 21
            [ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec);

这与我把所有东西都放在一个班级里有关吗?我以前从未在 matlab 中编程,如果这是一个相当明显的错误,请原谅我,但我无法弄清楚发生了什么。

【问题讨论】:

  • 我很确定 matlab 没有意识到该函数在类中 - 这是一个奇怪的设计选择。您需要使用类的名称调用该函数 - gaussfunctions.ut_reduce(A,B)。
  • 是的,我想在自己的文件中定义函数更合适,我已经习惯了 Java 和 C,以至于在编辑代码和查看函数如何交互时看起来很笨重。跨度>
  • 如果你认为这很糟糕......一旦加载了一个类,如果你更改文件,几乎不可能让Matlab“忘记”。我曾经发布过一个关于stackoverflow.com/questions/17242237/… 的有趣问题——它让你发疯。 如果你打算在 Matlab 中进行 OOP,忘记你对类的了解。

标签: matlab arguments double undefined-function


【解决方案1】:

MATLAB 与 C++ 和 Java® 等语言的不同之处在于没有 传递给所有方法的特殊隐藏类实例。你必须通过一个 类的对象显式地传递给方法。最左边的论点 不需要是类实例,参数列表可以有 多个对象。

来自http://www.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html

还有:

classdef 文件中的本地函数对于您仅在该文件中使用的实用程序函数很有用。这些函数可以取或返回 作为类实例的参数,但不是必需的,因为 在普通方法的情况下。例如下面的代码 在 classdef 块外定义 myUtilityFcn:

这就是说,如果你在实用函数之前放置一个end,它将在 classdef 之外,一切都会好起来的。它将在文件中,但在类之外。因此它对世界是“隐藏的”(在类定义之外看不到),但您的类可以使用它:

  % ... earlier code ...
        %compute the solution vector
        solution_vec = back_subst(ut_mat, new_const_vec);
    end
end  % <<<< add this one

function [ut_mat, new_const_vec] = ut_reduce(param_mat, const_vec)

%....
end
end % <<<<<< remove the last end statement

【讨论】:

    猜你喜欢
    • 2012-04-24
    • 2013-09-01
    • 2020-07-20
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多