- 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.
File: myFact.m Line: 11 Column: 1. This statement is not inside any function.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
function result = myFact(n)
if n == 0
result = 1;
else
result = 1;
for i = 1:n
result = result * i;
end
end
end
n = 5;
result = myFact(n);
disp(['The factorial of ', num2str(n), ' is: ', num2str(result)]);
Error: File: myFact.m Line: 11 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "myFact".)
How can I fix it? Please help.
0 Kommentare
Akzeptierte Antwort
Voss
am 3 Apr. 2024
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!