Basic GUI (Slider)

Basic GUI (Slider)

Final GUI for this tutorial

Initializing GUIDE (GUI Creator)

1. Open up MATLAB. Go to the command window and type in guide

2. Choose the first option Blank GUI (Default)

3. You should now see the following screen.

4. For the adder GUI, we will need the following components

Add an Edit Text component to the GUI figure.

Add a Slider component onto the GUI figure.

5. Edit the properties of these components.

Double click the Edit Text component to bring up the Property Inspector.

Change the String property to 0, and change the Tag property to slider_editText.

6. Modify the properties of the Slider component.

Sit the Min property to 0, and the Max property to 100.

Change the Tag property to slider1.

7. The figure should look like after you add the components and modify them.

8. Add some Static Text components to specify the min and max values of the slider.

Modify their text by double clicking on the component and changing the String property.

It’s not required, but I highly recommend it.

9. Save your GUI wherever you please with your desired filename.


Writing the Code for the GUI Callbacks

10. Open up the .m file that was automatically generated when you saved your GUI.

11. In the MATLAB editor, click on the icon, which will bring up a list of the functions within the .m file.

Select slider1_Callback.

12. Add the following code to the function:

Copy and paste the code

sliderValue = get(handles.slider1, ‘Value’);
set(handles.slider_editText, ‘String’, num2str(sliderValue));
guidata(hObject, handles);

13. Add the following code to the slider_editText_Callback function:

Copy and paste the code

sliderValue = get(handles.slider_editText, ’String’);
sliderValue = strnum(sliderValue);
if (isempty(sliderValue) || sliderValue < 0 || sliderValue > 100)
set (handles.slider1, ‘Value’,0);
set (handles.slider_editText, ‘String’,’0’);
else
   set (handles.slider1,’Value’,sliderValue);
end 

14. From the GUIDE editor, you can click BUTTON PLAY on the to launch the GUI.

15. Now, try to put in different types of inputs to test the GUI. Any input that is not a number, less than zero, or greater than 100 should default the slider to a value of zero.

END

izzad Ramli

Comments are closed.