Help please (button click determination)

1 Ansicht (letzte 30 Tage)
kaan yilmaz
kaan yilmaz am 2 Mär. 2012
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!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Mär. 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 Kommentare
kaan yilmaz
kaan yilmaz am 2 Mär. 2012
Buttons are created in GUIDE, not on the .m file, and they are all individual buttons (they symbolize the days of a year for a daily calendar)
i wonder if this src is defined as a function in Matlab since when i type >>help src , nothing is displayed..
Meanwhile thank you for your kind reply, but i think i will have a detailed study on this answer
Walter Roberson
Walter Roberson am 2 Mär. 2012
In GUIDE it would tend to be called hObject instead of src
http://www.mathworks.com/help/techdoc/creating_guis/f10-1000947.html

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by