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

1 次查看(过去 30 天)
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
KSSV 2020-11-21
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

更多回答(2 个)

Geoff Hayes
Geoff Hayes 2020-11-21
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

Steven Lord
Steven Lord 2020-11-21
编辑:Steven Lord 2020-11-21
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
  2 个评论
Muhammed Berat Eyigün
Yes, you were right. I fixed that one but still have one. When I execute the function it just goes to the value that at the end of the M and take that value into consider and makes the calculation for that value. I have just 1 display. But I need all the values, what should I do?
Muhammed Berat Eyigün
编辑:Muhammed Berat Eyigün 2020-11-21
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 5G Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by