필터 지우기
필터 지우기

Error using function: Not enough input arguments.

조회 수: 1 (최근 30일)
Astrik
Astrik 2016년 8월 5일
편집: John D'Errico 2016년 8월 5일
I get the following error every time i try to run a simple function
Not enough input arguments. My code is the following
function S = myfirstmatlab (n )
% gives the sum of the first n integers
S = 0; % start at zero
% The loop :
for i = 1: n % do n times
S = S + i; % add the current integer
end % end of the loop
Can someone help me to figure it out? The answers to similar questions did not help me.

채택된 답변

John D'Errico
John D'Errico 2016년 8월 5일
편집: John D'Errico 2016년 8월 5일
You don't RUN a function.
IMHO, run (or use of it from the menu) is the worst thing they ever introduced into MATLAB, in terms of newbies misusing the tool.
When you write a function, in order to use it, you need to use it at the command line, just as you use ANY OTHER FUNCTION.
So execute a line like this at the commandline:
S = myfirstmatlab(10);

추가 답변 (1개)

Thorsten
Thorsten 2016년 8월 5일
편집: Thorsten 2016년 8월 5일
You have to call the function with an argument n:
>> myfirstmatlab(4)
Note that you can write your program in a single line
S = sum(1:n);

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by