Help please (button click determination)

1 回表示 (過去 30 日間)
kaan yilmaz
kaan yilmaz 2012 年 3 月 2 日
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!

採用された回答

Walter Roberson
Walter Roberson 2012 年 3 月 2 日
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 件のコメント
kaan yilmaz
kaan yilmaz 2012 年 3 月 2 日
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 2012 年 3 月 2 日
In GUIDE it would tend to be called hObject instead of src
http://www.mathworks.com/help/techdoc/creating_guis/f10-1000947.html

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by