简体   繁体   English

MATLAB 输入不够 arguments

[英]MATLAB not enough input arguments

I keep trying to run this and have no idea what is going wrong.我一直在尝试运行它,但不知道出了什么问题。 I have it saved as test.m.我把它保存为 test.m。 I click run in the editor and in the matlab command window, it states not enough input arguments.我在编辑器和 matlab 命令 window 中单击运行,它指出输入 arguments 不足。 I feel like I am missing something totally obvious, but I cannot spot the issue.我觉得我错过了一些完全明显的东西,但我无法发现问题。

function y = test(A, x)
    %This function computes the product of matrix A by vector x row-wise
    % define m number of rows here to feed into for loop
    [ma,na] = size(A);
    [mx,nx] = size(x);
    % use if statement to check for proper dimensions
    if(na == mx && nx == 1)
        y = zeros(ma,1);   % initialize y vector 
        for n = 1:ma
            y(n) = A(n,:)*x;
        end
    else
       disp('Dimensions of matrices do not match')
       y = [];
    end
end

It is a function (not an script) and it needs some input arguments to run (in this case A and x ), so you cannot hit the run button and expect it to run.它是一个函数(不是脚本),它需要一些输入参数才能运行(在本例中为Ax ),因此您不能点击运行按钮并期望它运行。

The first way:第一种方式:

Instead you can use the command windows in MATLAB and enter the command:相反,您可以使用 MATLAB 中的命令窗口并输入命令:

A = rand(3,3); % define A here
x = ones(3,1); % define x here
test(A,x) % then run the function with its arguments

remember that A and x should be defined properly.请记住,应正确定义Ax

The second way is:第二种方式是:

Also you can hit the little triangle besides the green run button (see the figure below), and it will show you another option, type command to run .你也可以点击绿色运行按钮旁边的小三角形(见下图),它会显示另一个选项, type command to run And there you can directly enter the same command test(A,x) .在那里你可以直接输入相同的命令test(A,x) After that, each time you just hit enter for this function and it runs this command instead of only the test command without any argument.之后,每次您只需为该函数按下回车键,它就会运行此命令,而不仅仅是没有任何参数的test命令。

在此处输入图片说明

The third way:第三种方式:

function y = test(A, x)
%// TESTING CODE:
if nargin==0
    A = default_value_for_A;
    x = default_value_for_x;
end
... %// rest of the function code

This way allows you to "click the play button" and have your function run with no explicit input arguments.这种方式允许您“单击播放按钮”并让您的函数在没有显式输入参数的情况下运行。 However, be advised that such a method should only be used:但是,请注意,这种方法只能用于:

the reason why you get this error is because you run your code from this function script.出现此错误的原因是因为您从此函数脚本运行代码。 but you must run your code from your main script (the file that you invoke or use this function in it).但是您必须从主脚本(您在其中调用或使用此函数的文件)运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM