Need help with my function
조회 수: 1 (최근 30일)
이전 댓글 표시
How can I input my own function? I type function [dist,vel,acc]= free_fall(t)or anything just involving the word function and receive an error message reading "FUNCTION keyword use is invalid here. This might cause later messages about END". I save the function as the script. Can not seem to figure it out.
댓글 수: 0
채택된 답변
Jan
2013년 7월 29일
편집: Jan
2013년 7월 29일
Function definitions are not allowed inside the Command Window or insider an M-script file, but only inside a function-M-file. You can recognize them by the first word "function".
The functions are defined either as sub-functions, after the main function has finished, or as nested function inside another function.
function Out = MainFunction(In)
disp('I''m the main function');
Nested;
SubFunction;
Nested;
function Nested
disp('I''m nested'');
end
end
function SubFunction
disp('I''m the sub function');
end
If you use nested function or any function is closed by end, all functions in an M-file must be closed by a trailing end.
추가 답변 (1개)
David Sanchez
2013년 7월 29일
function [dist,vel,acc]= free_fall(t)
% The code of your function here
...
...
% no need of adding "end"
댓글 수: 2
David Sanchez
2013년 7월 29일
did you place your function in its own m-file, I mean, no any other code before the function declaration? Your error message is received when that is done. Functions has to be save in their own m-file.
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!