MATLAB Answers

what does the function handles do in matlab?

6 views (last 30 days)

Accepted Answer

Walter Roberson
Walter Roberson on 14 May 2021
handles is not a function. It is a struct.
handles by itself has no special meaning; it could have been called anything. The code could have been written as
gui_data.b = b;
gui_data.S = S;
provided that all the other references to handles were changed.
Using the name handles is a convention that was established by the GUIDE application builder.
MATLAB itself only passes two parameters to (most) callbacks: the object that that callback is for, and some details about why there was a callback.
But it is convenient to also have a bundle of information that gives quick access to all of the graphics objects instead of requiring that the program search for the desired object every time. Sort of like having a Contacts application instead of forcing people to look up phone numbers with Directory Assistance all of the time.
GUIDE maintains that kind of bundle of information, and intercepts the real MATLAB callback in order to add the bundle (struct) of information to the call, to make it easier to write graphics routines. The struct could have been called ToastedCheese and MATLAB would not have cared: as far as MATLAB is concerned, it is just another parameter being passed into the function.
The handles struct that you receive into any callback is a copy of the data bundle associated with the figure. You can modify the struct however you want, and nothing else will be affected -- not unless you tell MATLAB to update the master copy of the data bundle. Which is done by calling guidata() and passing in the handle to an object inside the figure, and passing in the new version of the data bundle. If you looked further in the code you would have seen something like
guidata(hObject, handles)
which is a call saying to update the master copy of the data structure associated with the figure that hObject belongs to.
Again, the exact name of the struct does not matter. The code could have been
guidata(hObject, ToastedCheese)
and MATLAB would have updated the master copy of the data bundle.
In context, what that section of code is doing is reading an image, resizing it, and saving a copy of the resized image, and a copy of the blue plane of the resized image, and (probably) then telling MATLAB to store the modified struct back over the master copy associated with the figure. Then the next time a callback is made, GUIDE would again have interecepted the real callback, and fetched the updated copy of the data bundle from the figure, and passed it in to the function, to be received there in a parameter that GUIDE choose to name handles -- but any consistent name could have been used.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!