How to make simple slider in GUI

1 view (last 30 days)
Scott on 1 Nov 2014
Answered: KATARI LOKESH on 1 May 2020
I am trying to make a simple slider switch in a GUI so that the user can select the bet amount they desire for a game of Blackjack. I would like for the 'callback' of this function to just return it to a variable that i have used elsewhere in the function.
Slider values: min: 100, max: 10000, starting value: 200, increment: 50
Here is essentially what I have so far, and it does not appear to be creating a slider at all. What am I doing wrong?
sld = uicontrol('Style', 'slider',...
'Position', [1 1.5 1. 1.5],...
'Callback', 'storebet(value)');
f.Visible = 'on';
function storebet(value)
basebet = value;
I have tried searching on Google and on this site and can't seem to find anything that works =(

Answers (3)

Image Analyst
Image Analyst on 1 Nov 2014
Image Analyst
Image Analyst on 1 Nov 2014
I don't know what's so hard or bad about creating a new blank GUI, dropping a slider onto it, setting it's min and max values, and putting code into the slider callback to retrieve the slider value into a variable. Seems ridiculously simple to me, but whatever, you can do it manually with uicontrol if you want.

Sign in to comment.

Jan on 1 Nov 2014
Edited: Jan on 1 Nov 2014
There are several misconceptions in this code:
  • 'Position', [1 1.5 1. 1.5] leads to a really tiny slider: It has a width of 1 and a height of 1.5 pixels.
  • Using a string as callback is not an error, but prone to bugs. Better use a function handle, as explained in Matlab's docs.
  • Inside the callback string the variable 'value' is accessed, but it is not defined. You cannot assume, that Matlab can guess, that you mean the value of the property 'Value'.
  • Inside the function storebet you set the variable basebet to the value of the input, but after this function is left, this local variable is deleted.
It would be better to call the callback with the usual 2 inputs and store the value in the ApplicationData of the figure, e.g. using guidata. This has been explained in hundreds of threads in this forum, such that a search will be useful.

KATARI LOKESH on 1 May 2020
clc;clear all;close all;
sld = uicontrol('Style', 'slider',...
'units','normalized', 'Position', [0.3 0.3 0.25 0.1]);
hi, Hope this code creates the slider for you


Find more on Just for fun 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!

Translated by