Help please (button click determination)
1 view (last 30 days)
Show older comments
I have 100 buttons in my gui, and i want the gui determine which button is pressed (namely, which button callback is operated), is this possible? The main thing i want to do is to change the colour of the button which is clicked, among 100 buttons. I tried some commands, and came up to this code but here all button colours are changed, not a specific one, help please... here is the button callback:
for k=1:100
h = handlesStructure.(sprintf('button%d', k));
set(h, 'BackgroundColor',[1 0 0]);
end
Maybe there should be a totally different code, i don't know, help please!
0 Comments
Accepted Answer
Walter Roberson
on 2 Mar 2012
Is this a uibuttongroup() collection, or is this a bunch of individual buttons?
If it is a bunch of individual buttons, then unless perhaps there is a method involving installing listeners, then the only callback that will be activated would be the callback for the individual button, whose callback would need to make the color change. The first parameter passed to the routine, usually called src, is the handle of the button that was activated.
curval = get(src,'Value');
maxval = get(src,'Max');
if curval == maxval
set(src, 'Color', [1.0 0.2 0.5]); %it is down
else
set(src, 'Color', [0.2 0.8 0.2]); %it is up
end
If you just want to be sure that "the current" button changes color, whatever the current button is, then the above kind of code will do it. If you care about identifying that (say) it was the "Morton Seasalt Blue #8" button specifically, then you may need more sophisticated coding.
2 Comments
Walter Roberson
on 2 Mar 2012
In GUIDE it would tend to be called hObject instead of src
http://www.mathworks.com/help/techdoc/creating_guis/f10-1000947.html
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!