Matlab GUI Tutorial - Slider
28 Oct 2007 Quan Quach 26 comments 12396 views
Writing the Code for the GUI
Matlab automatically generates an .m file to go along with the figure that you just put together. The .m file is where we attach the appropriate code to the callback of each component. For the purposes of this tutorial, we are primarily concerned only with the callback functions. You don’t have to worry about any of the other function types.
-
Open up the .m file that was automatically generated when you saved your GUI. In the Matlab editor, click on the
icon, which will bring up a list of the functions within the .m file. Select slider1_Callback. 
Add the following code to the function:
%obtains the slider value from the slider component sliderValue = get(handles.slider1,'Value'); %puts the slider value into the edit text component set(handles.slider_editText,'String', num2str(sliderValue)); % Update handles structure guidata(hObject, handles);
-
Now, let’s add the following code to the slider_editText_Callback function:
%get the string for the editText component sliderValue = get(handles.slider_editText,'String'); %convert from string to number if possible, otherwise returns empty sliderValue = str2num(sliderValue); %if user inputs something is not a number, or if the input is less than 0 %or greater than 100, then the slider value defaults to 0 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
-
Save your m-file!
Run and Test the GUI
Now that we’ve completed both the visual and code aspects of the GUI, its time to run the GUI to make sure it works.
-
From the m-file editor, you can click on the
icon to save and run the GUI. Alternatively, from the GUIDE editor, you can click on the
to launch the GUI. The following GUI should appear once you click the icon: 
-
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.
-
And that’s it. Those are the basics of using a Slider component. You can explore the other options that the slider has to offer through the Property Inspector. For instance, you can use the SliderStep property to customize how far you want the slider to move when you press the left and right arrow, or when you click on the scroll bar.
This is the end of the tutorial.
Source files can be downloaded here.
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