Matlab GUI Tutorial - Slider
28 Oct 2007 Quan Quach 26 comments 12392 views
Introduction
In this Matlab GUI tutorial, you will learn how to create and use the slider component. Sliders are useful controls for choosing a value in a range of values. Common uses are volume controls, seekers for movie and sound files as well as color pickers. An example of a slider is shown below.

This tutorial is written for those with little or no experience creating a Matlab GUI (Graphical User Interface). If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab is not required, but recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isn’t too outdated). Let’s get started!
Create the Visual Aspect of the GUI
-
Start up Matlab, and type
guidein the command line.
-
Choose to create a new GUI using the “Blank GUI(Default)” option.

-
Click on
and add an Edit Text component to the GUI figure. Next, click on
and add a Slider component onto the GUI figure. -
Double click the Edit Text component to bring up the Property Inspector. Change the String property to
0, and change the Tag property tosliderValue_editTextas shown in the figure below:
-
Next, let’s modify the properties of the Slider component. First lets sit the Min property to
0, and the Max property to100. Next, change the Tag property toslider1.
-
Here’s what your figure should look like after you add the components and modify them.

-
At this point, you also might want to add some Static Text components to specify the min and max values of the slider. You can modify their text by double clicking on the component and changing the String property. It’s not required, but I highly recommend it.

-
Save your GUI wherever you please with your desired filename.
Pages: 1 2
26 Responses to “Matlab GUI Tutorial - Slider”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

This was very helpfull, thnx 4 helping me out!
Thanks!!!! Am doing coursework - very helpful…
Very nice mate, clearly presented and easy to follow, 10/10
Thanks for the great tutorials I have been living on your tutorials for the past few days and they are helping me drive through… Great work and effort ….
I have some inquiries about the slider,
I would like to have a min value that is not zero on the slider such that the extreme left is a 1 for example. In addition I would like to know how I could set the initial value/ Position of the slider.
Thanks for your help
Hey Eman, thanks for the support… here are the answers to your inquiries
1. You can change min value of the slider by going to GUI builder and double click on the slider to have the properties menu show up. Scroll to Min, and change the value form 0 to 1.
2. To set initial value, here is one way to do it. Paste the following code in the Slider_OpeningFcn. It basically sets your slider value and the text box to an initialized value that you determine.
initval = 10;
set(handles.slider1,’Value’,initval);
set(handles.slider_editText,’String’, num2str(initval));
Thanks a lot for your help Its working Fine now
thanks! these tutorials are helpful
i would like to use the matlab GUI for drawing a graph of ECG signal (only 50 among 500 elements of the data should be displayed first) and to use a slider so that when user click the slider other parts of graph should be displayed just in the same way we use the normal slider. As user press up part of the slider a page moves up and vise versa. i hope you will help
How multiline static text can scroll using slider in GUI. Actually i have a big output to show on static text, not fessible to display on assigned place of Satic text. Scrolling required horizotally and vertically. I required tutorial as i am new in Matlab. Thanks in advance.
Nice work there! Is there any way of displaying the current value of the slider, on it, or aside of it?
Yiannis ==> You can add another “edit text” with a very little fontsize.
Then, you put it just under your slider and you repeat the tutorial steps to “connect” the slider and the new “edit text”.
sorry for my english
Nice Article
extremely good and simple to understand tutorials..not just this one…all of them…
thank you!!
Have you found a way to make the slider update constantly? I realize this would create loads of callbacks, but I find it necessary in some instances. I’ve found no documentation to support this feature.
Realy good work you done here. Helps a lot.
I’ve got a problem and I wonder if you got an idea how to solve it.
I’m using a slider that should cover the area from 0-63and I’m only interested in integers. I’ve connected it to an edit as you described.
I’ve found out that I should use 1/64 as sliderstep but the property inspector oly takes the decimals and thats give me 1/64~0.016 wich isn’t good enough.
Dose anybody know how to solve this?
Thanks…
Sorry about my unreadable writing.
I’ve found out that I should use 1/64 as sliderstep but the property inspector only takes three decimals and that gives me 1/64~0.016 wich isn’t good enough.
Yes, you have to use decimal from 0 to 1 which is ‘1 / (max - min)’. What you can do is use wincalc to determine what the step is and copy and paste the answer into the property editor for the slider x and y values (x is for the direction buttons of the slider and y is for the trough). When you click on it, it will show the full value (0.015625) until you click away then it will probable show what you are experiencing. This is not a problem, it should still work fine.
In the callback for the slider, you may want to ensure that you are getting integers by using something like the following:
If you have an edit box called textInput and a slider called textInputSlider,
for the slider
set(handles.textInputSlider, ‘String’, floor(get(hObject,’Value’)));
Thanks
Hello,
Your website helped me alot. I do not know how to say thank you. I have read quite a lot of text books on GUI. But the final implimentation of the GUI has not been properly explained except you. Looking forward to read more educative items from your website.
so i declared a global variable called “wavelength1″ in both edit text and slider callbacks. then i set “wavelength1 = sliderValue” so that the two are linked. my problem is that when i go to enter a value in the edittext and then hit enter it does a carriage return rather than changing the position of the slider. i found that if i enter a value in the edittext and then click somewhere else, the slider changes position. but how can i make it so that when i press enter in the edit text it will change the position?
Hi Kaspi,
I’m not sure why you would declare a global variable and link them. The way I coded it in this example has the text box and slider already linked. Try downloading the source code and playing around with it.
Quan
[...] You can download the sample input file here. (Right Click the link and use “Save As”) The sample file is the HTML code for the Slider Tutorial. [...]
QUan,
I linked them because i want either the slider and the box to output their values that i can work with them outside of their callbacks. (if i didn’t link them with a global variable, the text/slider combo would be pretty useless as i wouldn’t be able to use their values anywhere else). anyways, do you know of a way to update the text instantaneously? e.g. when i’m dragging the slider around, the textbox displays the value of the slider position.
also, do you know how to make it so that if i enter a value in the textbox and hit enter (on the keyboard) it would “be stored”. right now your sourcefile does a carriage return (new line) when i hit enter. thanks bud.
kaspi
Kaspi,
The handles structures that contains all the GUI components can be accessed in any part of your GUI code. This means you can access the slider value within any part of your gui by using:
If you wanted to access these files outside of the GUI framework, then that is a different matter.
I’ll try to answer the rest of your question later when I have access to MATLAB.
Quan
Thank you so much! I looked for hours through the MATLAB help to find a way to change the static text programmatically, and found nothing.
Is there a way to get slider values live, as the user drags the location box back and forth?
Thanks again,
Nathan
Thank you very much for this tutorial. It is very easy to understand and applicate.
I hope the remaining courses are also like this.
The tutorial on how to set up a slider is excellent. I would like to know how to set up multiple sliders so that I could plot a function as each slider (variable) changed.
Thank you,
John