MATLAB Answers

What does "@" do ?

1,472 views (last 30 days)
H ZETT M
H ZETT M on 12 Jan 2017
Commented: Eun-Mo Son on 13 Oct 2020
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 ?

  0 Comments

Sign in to comment.

Accepted Answer

Vandana Rajan
Vandana Rajan on 16 Jan 2017
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.
1. https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
2. https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
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.
https://www.mathworks.com/help/matlab/matlab_oop/organizing-classes-in-folders.html

  6 Comments

Show 3 older comments
Walter Roberson
Walter Roberson on 27 May 2020
https://stackoverflow.com/questions/5743291/python-function-handle-ala-matlab/10941428
Stephen Cobeldick
Stephen Cobeldick on 27 May 2020
"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 on 13 Oct 2020
Good. Cheers

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!