So this is a simple question. I found some code that uses "@" and I looked it up in the matlab documentation, but I did not find it to be really helpfull. Can somebody explain to me what it does and when/how I want to use it ?

 採用された回答

Vandana Rajan
Vandana Rajan 2017 年 1 月 16 日
編集済み: Walter Roberson 2023 年 10 月 6 日

15 投票

Hi,
You might have seen function handles.
A function handle is a MATLAB® data type that stores an association to a function.
To create a handle for a function, precede the function name with an @ sign. For example, if you have a function called myfunction, create a handle named f as follows:
>> f = @myfunction;
Now if you have a function like
function y = computeSquare(x)
y = x.^2;
end
>> f = @ComputerSquare;
>> a = 4;
>> b = f(a)
will give
b = 16
Please go through these 2 documentation links which very clearly explains about function handles.
Yet another use of the symbol '@' is as class folder designator. An @ sign can indicate the name of a class folder, such as
\@myclass\get.m
See the below link for more info.

10 件のコメント

H ZETT M
H ZETT M 2017 年 1 月 16 日
so basically everything it does is giving me the possibility to "rename" my function ?
Vandana Rajan
Vandana Rajan 2017 年 2 月 4 日
編集済み: Walter Roberson 2023 年 10 月 6 日
Not exactly. There are some advantages too.
1. While creating a function, you write it in a .m file and save it in the MATLAB path somewhere. But Anonymous functions can be created from the command line without a script.
2. A function handle can point to a function that is not in scope at the time of execution. For example, the function can be a subfunction in another M-file.
Please use these links for more insight into the advantages of using function handles and anonymous functions
wejden hammami
wejden hammami 2020 年 5 月 27 日
did you know the equivalent of @ in python ?
Walter Roberson
Walter Roberson 2020 年 5 月 27 日
編集済み: Walter Roberson 2022 年 8 月 23 日
Stephen23
Stephen23 2020 年 5 月 27 日
"did you know the equivalent of @ in python ?"
They are not needed in Python: unlike MATLAB every function must be called using parentheses (even if they contain no inputs), and so the function name (without parentheses) can simply be passed around as a variable (e.g. as input argument, assigned to another variable, etc.). You don't need to add anything else, just use the function name.
Eun-Mo Son
Eun-Mo Son 2020 年 10 月 13 日
Good. Cheers
tuan nguyen minh
tuan nguyen minh 2021 年 10 月 16 日
thanks so much. where can i find more example like this?
tuan nguyen minh
tuan nguyen minh 2021 年 10 月 16 日
i need more example or can you explain it to me?
FuFu=@(t, u1, u2)([u2, 4*t-2*u1-(4/5)*u2]);
function f=RK_ex1(t, u1, u2)
f = [u2,4*t-2.0*u1-(4/5)*u2];
end
Walter Roberson
Walter Roberson 2021 年 10 月 16 日
編集済み: Walter Roberson 2022 年 8 月 23 日
Rockford
Rockford 2022 年 8 月 23 日
It should be computeSquare, not ComputerSquare.

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

その他の回答 (1 件)

Amir Azadeh Ranjbar
Amir Azadeh Ranjbar 2023 年 10 月 6 日

3 投票

its function handle
func = @(x) x*2+10
func = function_handle with value:
@(x)x*2+10
func(5)
ans = 20

3 件のコメント

Walter Roberson
Walter Roberson 2023 年 10 月 6 日
Yes, as was explained by @Vandana Rajan 6 years ago.
Amir Azadeh Ranjbar
Amir Azadeh Ranjbar 2023 年 10 月 7 日
Well, this answer that was given (6 years ago) is more confusing....
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 7 日
What makes you think it is confusing? (let alone more confusing)
And your answer does not mention important details that the accepted answer provides.

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

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by