I'm keep having an error 'Not enough input arguments' for this code

2 views (last 30 days)
function [y] = DivisibleTest(M)
for m = 1 : length(M)
if mod(M(m),2)==0
y = ["%d is divisible by 2",m,num2str(M)];
elseif mod(M(m),3)==0
y = ["%d is divisible by 3",m,num2str(M)];
elseif mod(M(m),2)==0 && mod(M(m),3)==0
y = ["%d is divisible by 2 AND 3",m,num2str(M)];
else
y = ["%d is NOT divisible by 2 or 3",m,num2str(M)];
end
end
end

KSSV on 21 Nov 2020
It looks like you have opened the function and run the code by pressing F5 key or run button in the editor. This is not the way to call a function. You need to call the function either in the command window or in the script with proper input values.
Call the function in the command window s shwon below.
M = 10 ; % your input
y = DivisibleTest(M) ;
y
Muhammed Berat Eyigün on 21 Nov 2020
Yes, thank you very much body. Actually it looks like I have much to learn in Matlab :) Thanks again, god bless you.

Geoff Hayes on 21 Nov 2020
Muhammed - from the function signature
function [y] = DivisibleTest(M)
when calling this function, you need to provide an input parameter for M. I suspect that you are either running this code from the (MATLAB) File Editor, or are calling this function from the command line like
>> DivisibleTest
You need to call this function with an input like
>> DivisibleTest(42) % or whatever input you wish
Muhammed Berat Eyigün on 21 Nov 2020
Thank you too maan. Appreciate it :)

Steven Lord on 21 Nov 2020
Edited: Steven Lord on 21 Nov 2020
Others have commented on the fact that you need to call your function with an input argument. I want to point out a different problem that you have. One of your lines of code is unreachable. When you have code that looks like:
if condition1
doA
elseif condition2
doB
elseif condition3
doC
else
doD
end
exactly one of doA, doB, doC, and doD will execute. Even if something that satisfies condition 3 also satisfies condition 1, doA and doC will not both execute. If anything that satisfies condition 3 also satisfies condition 1 but not the reverse, you should check for condition 3 first.
if x < 3
disp('x is less than 3')
elseif x < 5
disp('x is less than 5')
elseif x < 10
disp('x is less than 10')
else
disp('x is greater than or equal to 10')
end
Try defining x to have various values between say 1 and 20 and run that example then do the same for the following example:
if x < 10
disp('x is less than 10')
elseif x < 5
disp('x is less than 5')
elseif x < 3
disp('x is less than 3')
else
disp('x is greater than or equal to 10')
end
Muhammed Berat Eyigün on 21 Nov 2020
function [y] = DivisibleTest(M)
for m = 1 : length(M)
if mod(M(m),2)==0 && mod(M(m),3)==0
y = fprintf("%d is divisible by 2 AND 3\n",M(m));
elseif mod(M(m),3)==0
y = fprintf("%d is divisible by 3\n",M(m));
elseif mod(M(m),2)==0
y = fprintf("%d is divisible by 2\n",M(m));
else
y = fprintf("%d is NOT divisible by 2 AND 3\n",M(m));
end
end
end
This is the last version of the code, fixed all the previous problems, now I have just last one :) Code makes the everything I want but at the end gives y value equals some value I dont understand. For example;
M=[12,45,78,96,10,3,8];
>> y=DivisibleTest(M)
12 is divisible by 2 AND 3
45 is divisible by 3
78 is divisible by 2 AND 3
96 is divisible by 2 AND 3
10 is divisible by 2
3 is divisible by 3
8 is divisible by 2
y =
20