Parse error at Function:usage might be invalid syntax

7 views (last 30 days)
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.

Answers (2)

Walter Roberson
Walter Roberson on 26 Oct 2016
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 Comment
Benjamin Azrieli
Benjamin Azrieli on 23 Nov 2020
Edited: Benjamin Azrieli on 23 Nov 2020
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.

Sign in to comment.


James Tursa
James Tursa on 26 Oct 2016
Edited: James Tursa on 26 Oct 2016
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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!