Why am i recieving a "parse" error?

30 ビュー (過去 30 日間)
Erin Cooper
Erin Cooper 2015 年 2 月 11 日
回答済み: Ankita Pawar 2019 年 9 月 10 日
I am trying to write a very simple function file to use in my more complicated script file. The file I have is this:
>function y(x)=trigC(x)
>>>y(x)=(cos(5*x))
>end
When I do this, the error within the function says "Parse error at '=': usage might be invalid MATLAB synax" referring to the y(x)=trigC(x). And when I run the program it says "The expression to the left of the equals sign is not a valid target for an assignment."
I am not familiar enough with MATLAB syntax to figure out what it even means, let alone how to fix it. Any ideas? Thank you!

採用された回答

Roger Stafford
Roger Stafford 2015 年 2 月 12 日
You can't write your function that way. Write it this way:
function y = trigC(x)
y = cos(5*x);
end
Because of the nature of the 'cos' function, if x is a vector of values, then y will return as a similar size vector of the cosine of five times the corresponding values in x. A similar statement holds if x is a scalar or an n-dimensional array.
  1 件のコメント
Erin Cooper
Erin Cooper 2015 年 2 月 12 日
That worked! Thank you

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

その他の回答 (1 件)

Ankita Pawar
Ankita Pawar 2019 年 9 月 10 日
i am trying to write this function but parse error occure near '=' this .
please tell me what is the wrong in it.
machine1
en:
switch = 1;
stack_m = 1;
stack_l = 1;
sensor_m1 = 1;
sensor_m2 = 1;
sensor_l1 = 1;
sensor_l2 = 1;
exit:
m = 1;

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by