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
댓글 수: 0
채택된 답변
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
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
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!