필터 지우기
필터 지우기

I am trying to programmatically create some GUI pushbuttons, but I am unable to retrieve the values passed in the callback. I want to find out the value and string attribute of the button pressed.

조회 수: 2 (최근 30일)
function autobuttongenrate
f = figure;
numOfButtons = 7;
p = uipanel(f,'Title','My Panel',...
'Position',[0 0 .3 1]);
buttonHeightPercent = (100/(numOfButtons + (numOfButtons*0.1)))/100;
unitDistanceBtwButtons = ((100 - (buttonHeightPercent*100*numOfButtons))/(numOfButtons+1))/100;
bArr = zeros(1, numOfButtons);
for n = 1:numOfButtons
newButtonButtomMargin = 1 - ((unitDistanceBtwButtons + buttonHeightPercent) * n);
bnew = uicontrol(p,'Style','pushbutton','String',n,...
'Value',n,...
'Units','normalized',...
'Position',[.1 newButtonButtomMargin .8 buttonHeightPercent],...
'Callback', @setmap);
bArr(n) = bnew;
end
ax = axes('Parent',f,'Position',[.4 .25 .5 .5]);
function setmap(source,event)
src = source;
eve = event;
h = msgbox({'pushbutton pressed'});
end
end

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 3일
If you are using R2014b or later, you need to replace
bArr = zeros(1, numOfButtons);
with
bArr = gobjects(1, numOfButtons);
Your callback should be something more like
function setmap(source,event)
button_string = get(source, 'String');
button_value = get(source, 'Value');
h = msgbox( sprintf('Button with string "%s" now has value %d', button_string, button_value);
end
If you are using R2014b or later this can be written as
function setmap(source,event)
button_string = source.String;
button_value = source.Value;
h = msgbox( sprintf('Button with string "%s" now has value %d', button_string, button_value);
end
Note: The _Value' property of a uicontrol style 'pushbutton' will remain what you originally configured it as, 'Value',n, until the button is pressed. During the Callback that is processed upon the button being pressed, the Value property will have the same number as the Max property, which defaults to 1 -- so during that callback the Value will be 1. Then as soon as that callback finishes, the Value property will be set to the same number as the Min property, which defaults to 0 -- so after the callback the Value will be 0. This 1 (Max) and 0 (Min) applies no matter what Value you originally configured with, so there is probably no point in setting the Value to n originally. If you want the 1 to persist then consider using a 'toggle' instead of a push: toggle switch between Min and Max values each time they are pressed.
Also: if you have reason to look directly at a pushbutton, you might want to consider giving it a Tag, as you can then findobj() on the Tag. On the other hand, it is more efficient to store them in an array and index the array -- you just have to save the array somewhere... such as in the handles structure.
  댓글 수: 6
Stephen23
Stephen23 2017년 6월 5일
"is there a better way to find out which among the buttons is pressed?"
A simple anonymous function does the trick:
for n = ...
bnew = uicontrol(p,...,'Callback', @(h,e)setmap(n)
end
and
function setmap(n)
fprintf('button %d was pressed.\n',n)
end
Abhay Aradhya
Abhay Aradhya 2017년 6월 5일
Thank you Walter and Stephen both the methods worked well. Great leanings.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by