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
  댓글 수: 1
Muhammed Berat Eyigün
Muhammed Berat Eyigün 2020년 11월 21일
Yes, thank you very much body. Actually it looks like I have much to learn in Matlab :) Thanks again, god bless you.

댓글을 달려면 로그인하십시오.

추가 답변 (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
Muhammed Berat Eyigün 2020년 11월 21일
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일
편집: 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