GUI listbox example file of matlab: A structure 'handles' is being used throughout although it hasnt been defined in the program? I don't understand how is this happening?

1 view (last 30 days)
The structure handles in the lbox2.m example is being rigorously used. The consider following code segment:
1 function load_listbox(dir_path,handles)
2 cd (dir_path)
3 dir_struct = dir(dir_path);
4 [sorted_names,sorted_index] = sortrows({dir_struct.name}');
5 handles.file_names = sorted_names;
6 handles.is_dir = [dir_struct.isdir];
7 handles.sorted_index = sorted_index;
8 guidata(handles.figure1,handles)
9 set(handles.listbox1,'String',handles.file_names,...
10 'Value',1)
11 set(handles.text1,'String',pwd)
see line 5,6,7 here fields in the structure handles are being used but i cannot see the structure being defined . How is this happening???????
NOTE :Please refer this link. If has the documentation of the example I am talking about http://in.mathworks.com/help/matlab/creating_guis/interactive-list-box-in-a-guide-gui.html#brbirpn

Answers (1)

Adam
Adam on 2 Apr 2015
A GUIDE-created UI will always have a handles structure embedded in it which is passed to every callback and which stores the handles to all UI components. You can also attacj your own data to handles if you want since it is always passed to callbacks to facilitate shared data in a GUI.
  2 Comments
Vikram Bisht
Vikram Bisht on 2 Apr 2015
So you whAt you want to say is that if i want to pass the value of a variable example- 'answer' , between different functions all i need to do is upload it to handles using handles.answer and later wherever i want to access it i can again obtain it using the same expression ,RIGHT?
Adam
Adam on 2 Apr 2015
Yes, but make sure you include the important line:
handles.answer = someFunction() % Whatever you want to store
guidata( hObject, handles )
That last line is what commits the handles structure back into the GUI. handles is simply a struct which is passed into the workspace of your callback function. i.e. it has function scope and any changes you make to it will simply be lost when the function exists if you do not include the guidata line at the end of your function.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!