Parse error at Function:usage might be invalid syntax

7 次查看(过去 30 天)
Can anyone tell me why I am getting a "Parse error at Function" message? The message in question is on the bottom of this image.

回答(2 个)

Walter Roberson
Walter Roberson 2016-10-26
You need an 'end' statement before that 'function', to match the 'function' on your first line (but which we cannot see in the picture). You have used an 'end' for the 'function' that we can see, and you have to be consistent about using (or not using) 'end' with every function in a .m file.
  1 个评论
Benjamin Azrieli
Benjamin Azrieli 2020-11-23
编辑:Benjamin Azrieli 2020-11-23
This was the solution for me when I encountered the same issue with functions in my script. I was working on a for-loop in the script and tried to use the function, located in the same script, in the command line without putting an end on the unfinished for-loop. Adding an end statement worked perfectly, thank you Walter.

请先登录,再进行评论。


James Tursa
James Tursa 2016-10-26
编辑:James Tursa 2016-10-26
Very hard to read your pic of the code (in the future, post the actual code and not a picture), but from the looks of it you have defined your own function named "eq". This is a very poor choice for a user-defined function name since that is the name of the MATLAB operator ==. So my guess is you are shadowing the MATLAB == operator with your eq function. Try picking a different name for your eq function, e.g. "myequation" or something else, and then see what errors you are getting with your code.
doc eq
And "error" is the name of another existing MATLAB function, so my advice is to use another name for that variable as well.
Finally, is this code part of a script file or a function file? If it is a script file, your version of MATLAB may not allow you to define functions within that file (i.e., your "eq" function). So you would need to put your "eq" function code (renamed to "myfunction" of course!) in a separate file.

类别

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