Hi,
I m trying to enter some new functions in my program and i keep getting "Parse error at x : usage might be invalid MATLABsyntax" when defining functions.
This one is an example i don t know what is wrong with it but it won't work
f = (@x) x+2
"invalid expression. Check for missing multipication operator..."
Can some of you help me please?

 採用された回答

Dyuman Joshi
Dyuman Joshi 2024 年 2 月 22 日

0 投票

The left parenthesis is supposed to be after the '@' symbol -
f = @(x) x +2
f = function_handle with value:
@(x)x+2
f(3)
ans = 5
The general syntax is
name_of_function = @(independent_variables_separated_by_comma) relation_of_independent_variables
%e.g - function of 3 independent variables
g = @(x,y,z) x + y.*z
%same can be written as
G = @(X,Y,Z) X + Y.*Z
For more information, check out the following documentation pages - Function Handle

5 件のコメント

Grégoire
Grégoire 2024 年 2 月 22 日
oh right !
Thanks you :)
Dyuman Joshi
Dyuman Joshi 2024 年 2 月 22 日
You're welcome!
Grégoire
Grégoire 2024 年 2 月 22 日
編集済み: Grégoire 2024 年 2 月 22 日
Sorry to bother again
I am getting the same type of error again even with the parenthesis right
g1 = @(x) 2/((14x-7).^(6/7))
"A '(' might be missing a closing ')', causing an invalid syntax at 'x'
I've checked my code and every () is closed...
any idea please ?
Dyuman Joshi
Dyuman Joshi 2024 年 2 月 22 日
You forgot to include the multiplication sign between 14 and x.
Note that MATLAB does not support implied multiplication, you have to specify the symbol.
Grégoire
Grégoire 2024 年 2 月 22 日
Okay noted,
thanks a lot for the help :)

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

その他の回答 (1 件)

Abhinay
Abhinay 2024 年 2 月 22 日
編集済み: Abhinay 2024 年 2 月 22 日

0 投票

I understand that you are getting an error "Parse error at x : usage might be invalid MATLABsyntax" when defining functions when you are trying to enter some new functions in your program.
The error you're seeing is because of a small mistake in the way you've written your function. To fix it, just remove the space between `@` and `(x)`. Here's how it should look:
f = @(x) x + 2;
Use this corrected line in your program, and it should work without the "Parse error".

カテゴリ

製品

リリース

R2023b

タグ

質問済み:

2024 年 2 月 22 日

編集済み:

2024 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by