How to make simple slider in GUI
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 =(
Réponses (3)
Image Analyst
le 1 Nov 2014
See this simple framework you can modify: http://www.mathworks.com/matlabcentral/fileexchange/24224-magic-matlab-generic-imaging-component
2 commentaires
Image Analyst
le 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.
Jan
le 1 Nov 2014
Modifié(e) : Jan
le 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.
0 commentaires
KATARI LOKESH
le 1 Mai 2020
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
0 commentaires
Voir également
Catégories
En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!