Help please (button click determination)

조회 수: 3 (최근 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 CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by