How to make simple slider in GUI

5 ビュー (過去 30 日間)
Scott
Scott 2014 年 11 月 1 日
回答済み: KATARI LOKESH 2020 年 5 月 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 =(
  1 件のコメント
Jan
Jan 2014 年 11 月 1 日
max 10000 or 1000?

サインインしてコメントする。

回答 (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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by