MATLAB Creating Graphical User Interfaces

(Barry) #1

8 Programming a GUIDE UI


% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine...
slider_value = get(hObject,'Value');
display(slider_value);

When the end user moves the slider, the callback function gets the current value of the
slider and displays it in the Command Window. By default, the slider’s range is [0, 1]. To
modify the range, set the slider’s Max and Min properties to the maximum and minimum
values, respectively.

List Box


Populate Items in the List Box

If you are developing a UI using GUIDE, use the list box CreateFcn callback to add
items to the list box.

This code is an example of a list box CreateFcn callback that populates the list box with
the items, Red, Green, and Blue.

function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns

% Hint: listbox controls usually have a white background on Windows.
if ispc && isequal(get(hObject,'BackgroundColor'), ...
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',{'Red';'Green';'Blue'});
The last line, set(hObject,'String',{'Red';'Green';'Blue'}), populates the
contents of the list box.

If you are developing a UI programmatically (without GUIDE), then populate the list box
when you create it. For example:

function myui()
Free download pdf