Reputation: 174
I am unable to write a function to generate a matrix. I keep on getting the error "Not enough input arguments" when I try to run the following:
function B=generate_matrix(n) B=zeros(n,n); %I'll add more stuff to the function once I can get it to work end
Does anyone know what I'm doing wrong? I've checked online already and don't know what I'm doing wrong.
Upvotes: 0
Views: 92
Reputation: 5170
The error message you have is perfectly normal.
You have defined a function which requires one input:
function B = generate_matrix(n)
B = zeros(n,n);
When you press the Run
button (or F5
), Matlab attempts to execute the code. Or Matlab does not know what is n
, hence the error message. Actually, the Run
button only works for scripts or functions with no inputs.
So, to use your function you have to call it (e.g. from the workspace) and specify an argument:
>> generate_matrix(3)
ans =
0 0 0
0 0 0
0 0 0
Note that this argument can be a variable that has been previously defined.
Upvotes: 2
Reputation: 20
Write file for the function
function B = generate_matrix(n)
B = zeros(n,n);
end
then, in the command window,
B = generate_matrix(3)
this should generate the 3x3 zero matrix. I think your code is working.
Upvotes: 0
Reputation: 114
Make sure your give input for the variable n. If n is null you will get this error. In the command window first assign value to n. Say n = 5
. Then run your program. Since n already has a value, your program will work fine.
Upvotes: 0