MATLAB Answers

the meaning of basic matlab words

19 views (last 30 days)
can any one explain to me the meaning of the "handle" word in details.
because I see this word in many matlab documentation, and then the things get complicated.


Adam on 13 Feb 2020
Beware of the fundamental difference between 'handle' and 'handles', which you will likely see both of. Image Analyst has given a description of what a 'handle' is. It's the same kind of concept as a jug or cup handle, in the sense of allowing you to use the object easily. If you ever do object-oriented programming in Matlab then a 'handle' class has some key properties that are different from a default (by value) class.
Chief amongst these is that they are passed around by reference, which is not standard behaviour for Matlab objects. Graphics objects, where you will most often interact with the concept of handles, have this property.
'handles', on the other hand, is something you will hear more in relation to GUIs created using GUIDE - a tool that is no longer recommended for use, but still works perfectly well and, for many of us, better than app designer currently.
In GUIDE, 'handles' simply refers to a struct which contains the handles of all the GUI components. It is important to note that this behaves like any other struct - i.e. it does not have 'pass-by-reference' semantics itself, even though the GUI handles it contains do.
Osama Al-Kurdi
Osama Al-Kurdi on 13 Feb 2020
did you mean that 'handle' is like cup that stores properties of an certain kind object and I can't interact with these properties, while there is other objects have a certain properties need a cup of 'handles' type.
Stephen Cobeldick
Stephen Cobeldick on 13 Feb 2020
"did you mean that 'handle' is like cup that stores properties of an certain kind object and I can't interact with these properties"
Not at all.
As Adam wrote, a handle is like a cup handle, it is not like the cup itself. A handle is a way to reference an object, but it is not the object itself. You can certainly interact with the properties of an object you have a handle of.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 13 Feb 2020
The word "handle" has at least two different (somewhat related) meanings in MATLAB.
A handle object is an object where different copies can refer to the same underlying "thing". So in Image Analyst's example, hFig is a handle object to the figure window on screen.
hFig = figure;
hFig2 = hFig;
Changing a property of hFig affects the (one) figure window on screen, and the property value of hFig2 reflects that change (and vice versa.)
before = hFig.Color
hFig2.Color = 'r';
after = hFig.Color
Even though it doesn't look like I did anything to hFig, I did do something to the underlying window on screen and so before and after are not the same.
See this documentation page for more information on how handle objects behave. Using handle ojects can be a bit of an advanced skill (using graphics objects to get and set properties of the underlying things on screen is a bit simpler, see this page for more information on how to do that) and writing your own handle classes is even more advanced.
A function handle is kind of sort of conceptually a handle object that refers to a function. I can use it to evaluate that function without hard-coding a call to the function into my code. Note that in the code below, just by changing fh and executing the exact same code I can do two very different things.
fh = @plot;
fh(1:10, 1:10)
fh = @plus;
fh(1:10, 1:10)
You can use the idea of a telephone number as an analogy for both of these meanings of handle. If I have your telephone number, and so does Image Analyst, that doesn't mean there are two copies of you running aroung the world. I could call you up and tell you something, then if Image Analyst were to call you you could tell them what I told you. Your telephone number is kind of a "handle to you".
Similarly, I'd get different behavior if I were to call you and say "I'd like to order a pizza for delivery" than if I called the local pizza place and give them the same message. Same data, different "function", different behavior.
For more information on function handles, see this documentation page. Often I think people's first or early experiences with function handles usually come with the ODE solvers, where the user enters a function handle as input so the solvers don't have a function hard-coded inside them.


Osama Al-Kurdi
Osama Al-Kurdi on 13 Feb 2020
your explanation was amazing.
thank you very much.
I wished if the all answering my questions in a simple way like this.
Steven Lord
Steven Lord on 13 Feb 2020
As Stephen Cobeldick mentioned on another question you asked you may want to work through some of the introductory documentation, tutorials, and/or training material to familiarize yourself with the basics of MATLAB. While not all of them will talk about object-oriented programming in MATLAB (as I said, it's a little bit of an advanced maneuver) they may help you develop a foundation upon which you can build with lessons from the rest of the documentation.
You can start from the MathWorks Support site. The Tutorials section includes videos, links to examples and documentation, and interactive courses. The MATLAB Onramp course is free and takes about 2 hours. You can do it online.
If you open the Documentation section of the Support site, the documentation for MATLAB has a chapter titled "Getting Started with MATLAB". That would probably be my next step in building the foundation of your MATLAB knowledge.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Feb 2020
Usually it's basically a variable that contains everything you need to interact with something, such as a graphical object displayed on your screen. For example gcf is the handle to the current figure (the whole window), and gca is the handle to whatever axes object on that figure was drawn to last. You can use those to set all kinds of properties, like to maximize the figure:
% Create a new figure and store its handle in a variable (an object) called hFig.
hFig = figure
% Maximize that figure.
hFig.WindowState = 'maximized'

  1 Comment

Osama Al-Kurdi
Osama Al-Kurdi on 13 Feb 2020
when you say a variable
did you mean it is like
the value one stored in the variable x?

Sign in to comment.

Sign in to answer this question.