How to make simple slider in GUI

조회 수: 2 (최근 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 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