Save the output of a pushed button
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone
I created several buttons within a figure using the uicontrol function. The subject, moving the mouse, has to press one button out of 6. Now, I would like to save which button the subject has chosen, but I have no idea how to do it.
Any help will be appreciated.
Thank you in advance!
0 commentaires
Réponse acceptée
Adam
le 2 Mar 2015
Modifié(e) : Adam
le 2 Mar 2015
fCallback = @(src,evt) disp( src.String );
figure; hButton1 = uicontrol( 'Units', 'normalized', 'Position', [0.1 0.1 0.35 0.1], 'Style', 'pushbutton', 'Tag', 'button1', 'String', 'Button 1', 'callback', fCallback );
hButton2 = uicontrol( 'Units', 'normalized', 'Position', [0.55 0.1 0.35 0.1], 'Style', 'pushbutton', 'Tag', 'button2', 'String', 'Button 2', 'callback', fCallback );
should give an example of what you want. It isn't necessary to set a Tag, but you can use either the tag or the string to determine which button was pressed if you do set a tag. My example ignores the tag and outputs the string, but strings are not guaranteed to be unique, although in your case I guess they will be.
Obviously you then need to save the information, but you haven't given any details as to what kind of code you are putting these into.
3 commentaires
Adam
le 3 Mar 2015
You will need to replace the callback I gave you with a normal function (i.e. not an anonymous function like that) that stores the result of which button was pressed somewhere. Where that value would be stored depends entirely on what you want to do with it and where this code is sitting - e.g. is it called from a function?
I can't see why your code would result in an error though even though it doesn't do what you would need it to.
hButton1.String will always be 'HAPPINESS' since that is what you created it as for the first button.
Incidentally, use strcmp( hButton1.String, 'HAPPINESS' ) to compare strings, not ==
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks 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!