Effacer les filtres
Effacer les filtres

Need help with my function

1 vue (au cours des 30 derniers jours)
Richard
Richard le 29 Juil 2013
Commenté : Joel Sande le 7 Avr 2016
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.

Réponse acceptée

Jan
Jan le 29 Juil 2013
Modifié(e) : Jan le 29 Juil 2013
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 commentaire
Joel Sande
Joel Sande le 7 Avr 2016
Thanks !

Connectez-vous pour commenter.

Plus de réponses (1)

David Sanchez
David Sanchez le 29 Juil 2013
function [dist,vel,acc]= free_fall(t)
% The code of your function here
...
...
% no need of adding "end"
  2 commentaires
Richard
Richard le 29 Juil 2013
No, I didn't place the end on my code that is what the error message reads. When I run the code it also reads "Function definitions are not permitted in this context". in the command window. Thanks for trying though.
David Sanchez
David Sanchez le 29 Juil 2013
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by