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.

1 回表示 (過去 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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by