Why is it that when I am trying to create a function, the run button at the top turns grey?

3 ビュー (過去 30 日間)
Hello. I am trying to create a user-defined function containing a function call, F=temp_conv(C), that will convert the temperature from Celsius to Fahrenheit and evaluated at C=27. However, whenever I type function, the run arrow at the top turns grey. If anyone could offer any tips or suggestions, it would be very helpful. Thank you.

回答 (2 件)

John D'Errico
John D'Errico 2022 年 9 月 25 日
編集済み: John D'Errico 2022 年 9 月 25 日
You DON'T use run on a function. That makes no sense I'm afraid.
Instead, functions in MATLAB are things you would call from either a script, or another function. But no matter what, you cannot run a function, and therefore, the ability to use run is disabled once you create it as a function.
Compare that instead to when you write a script. A script is just a list of lines of code, as if you would write them sequentially in the command window. A script uses the base MATLAB workspace.
A function has its own workspace. You pass it arguments, which are the only things it sees. (Nested functions are a subtly different animal, but even so, they cannot be "run" either.) A function returns results, as arguments. It cannot be run like a script.

Image Analyst
Image Analyst 2022 年 9 月 25 日
Try creating a regular script, not a live script. I tried it and the run button is enabled. However you need to have your function code lines defined, AFTER your script lines, not before like you have it. The "end" you have finishes off the function, then you're calling the function after that. In MATLAB you need the reverse. Call the function in the first lines (top of the file) then define your function(s) after your script program (if you're going to combine them in a single .m file) or else put your functions into their own m-files.
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 25 日
The line after the "end" is the cause of the difficulty, I believe. MATLAB is detecting that the line is invalid at that location, a syntax error, and refusing to run the file until the syntax error gets fixed (by moving that last line to before the function declaration)

サインインしてコメントする。

カテゴリ

Find more on Desktop 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!

Translated by