- Remove the lines that are not in the function definition (i.e., the three lines after the last end), which makes your m-file a function file, and then run those lines in the command window or put them in a script, or
- Move those three lines to the top of your m-file, making your m-file a script.
This statement is not inside any function.
조회 수: 37 (최근 30일)
이전 댓글 표시
function sumVector = computeSum(vector)
sumVector = 0;
for i = 1:length(vector)
sumVector = sumVector + vector(i);
end
end
V = 1:10;
totalSum = computeSum(V);
disp(['Sum of elements in vector V is ' num2str(totalSum)]);
Error: File: computeSum.m Line: 9 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "computeSum".)
댓글 수: 0
채택된 답변
Voss
2024년 4월 3일
If the first executable line of code (that is, the first line that's not a comment) in an m-file is a function declaration, then that m-file is a function file and cannot contain anything besides that function or other local functions (a.k.a. subfunctions).
If the first executable line of code in an m-file is not a function declaration, then that m-file is a script and can contain functions and other lines of code not in functions (and until very recently all function definitions must be at the end of the script).
So your options are to:
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!