Function requires more input arguments to run

106 ビュー (過去 30 日間)
Cosimo Iurlaro
Cosimo Iurlaro 2017 年 3 月 20 日
コメント済み: Steven Lord 2019 年 7 月 5 日
My function is:
function[fa]=speriamo(u)
if (0<=u<pi/3)
fa=1; end;
if (pi/3<=u<2*pi/3)
fa=1; end;
if (2*pi/3<=u<pi)
fa=-6*u/pi+5; end;
if (pi<=u<4*pi/3)
fa=-1; end;
if (4*pi/3<=u<5*pi/3)
fa=-1; end;
if (5*pi/3<=u<2*pi)
fa=6*u/pi-11; end;
end
when I click "Run", the program give me error: "speriamo" requires more input arguments to run. Any other variable I would add is regarded as an unnecessary variable. It's a simple function, what am I doing wrong?
  4 件のコメント
Adam
Adam 2017 年 3 月 20 日
Both of the answers below work fine to solve the problem, I am just trying to get you to think logically about it so that you don't do the same thing again.
If the whole point of your function is to run some code based on an input parameter surely the first thing you should be thinking about when running the function is to be able to pass in that input parameter, otherwise what kind of results could you expect?
Cosimo Iurlaro
Cosimo Iurlaro 2017 年 3 月 20 日
thank you very much.

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

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 3 月 20 日
Cosimo - since this is a function that has one input parameter, then you need to supply that parameter. From the command line you would do
>> [fa]=speriamo(42);
Pressing the RUN button (from the MATLAB file editor) will just call the function as
>> speriamo
and not provide any input parameters. Hence the error.
  1 件のコメント
Cosimo Iurlaro
Cosimo Iurlaro 2017 年 3 月 20 日
thank you, very much.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2017 年 3 月 20 日
When you click Run you call your function with 0 input arguments. The way you have defined the function:
function[fa]=speriamo(u)
it requires one input argument to run. See this page from the documentation for how to configure the Run button to run your function with an input argument, or call your function at the MATLAB command prompt (the >> or EDU>> prompt in the Command Window) with an input argument.
FYI, this line of code:
if (0<=u<pi/3)
doesn't do what you think it does. If you're only going to run this code with a scalar (1-by-1) u then rewrite it as:
if (0 <= u) & (u < pi/3)
If you're going to run this code with a nonscalar u, use logical indexing.
  3 件のコメント
jakkapally srinivas
jakkapally srinivas 2019 年 7 月 5 日
Do you even know what you have answered!
Steven Lord
Steven Lord 2019 年 7 月 5 日
Yes.

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

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by