I keep trying to run this and have no idea what is going wrong. I have it saved as test.m. I click run in the editor and in the matlab command window, it states not enough input 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
1 By using the Command Prompt: This is a very simple method to solve the not enough input argument error. In this method, we simply create the input whatever we require on the command prompt, and after that, we need to execute that input by using the function or script that we already write.
If your function requires input arguments, the Not enough input arguments error will occur as you have written a functions that expects inputs to go inside the function. Therefore, you cannot expect the function to run by simply pushing the Run button.
Common causes: 1) You have passed a function more input arguments than it expected to receive, perhaps by passing a list of inputs rather than a vector of inputs, or have tried to obtain two outputs from a function that only returns one. 2) You have multiple functions with the same name.
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.
Instead you can use the command windows in MATLAB and enter the command:
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.
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
. And
there you can directly enter the same command 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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With