Parse error at Function:usage might be invalid syntax

7 ビュー (過去 30 日間)
juliet
juliet 2016 年 10 月 25 日
編集済み: Benjamin Azrieli 2020 年 11 月 23 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by