How to make simple slider in GUI

2 次查看(过去 30 天)
Scott
Scott 2014-11-1
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',...
'Min',100,'Max',1000,'Value',100,...
'Position', [1 1.5 1. 1.5],...
'Callback', 'storebet(value)');
f.Visible = 'on';
function storebet(value)
basebet = value;
end
I have tried searching on Google and on this site and can't seem to find anything that works =(

回答(3 个)

Image Analyst
Image Analyst 2014-11-1
  2 个评论
Scott
Scott 2014-11-1
well those resources didn't actually help much. i just need a simple command which will create a slider between 100-10000 that stores the value from the slider in a variable
Image Analyst
Image Analyst 2014-11-1
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.

请先登录,再进行评论。


Jan
Jan 2014-11-1
编辑:Jan 2014-11-1
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
KATARI LOKESH 2020-5-1
clc;clear all;close all;
sld = uicontrol('Style', 'slider',...
'Max',10,'Min',1,'Value',1,...
'units','normalized', 'Position', [0.3 0.3 0.25 0.1]);
hi, Hope this code creates the slider for you

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by