I'm keep having an error 'Not enough input arguments' for this code
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (2)
Geoff Hayes
am 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
Steven Lord
am 21 Nov. 2020
Bearbeitet: Steven Lord
am 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
2 Kommentare
Muhammed Berat Eyigün
am 21 Nov. 2020
Muhammed Berat Eyigün
am 21 Nov. 2020
Bearbeitet: Muhammed Berat Eyigün
am 21 Nov. 2020
Kategorien
Mehr zu Command Line Only finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!