Keep track of iterator in different pushbuttons

Hello all,
Being part of a larger project, I wish to iterate over a bunch of pushbuttons on a Figure Window, and display the iterator number on the Command Window. I have written something like
for iter = 1 : 5
uicontrol('style', 'pushbutton', 'position', [20 iter*30 50 20], 'string', 'Open...', 'callback', 'iter')
end
but this gives always
iter = 5
when invoking any of the five pushbuttons. What am I missing here?
Thanks for all the help! Gabriel

 Accepted Answer

for iter = 1 : 5
uicontrol('style', 'pushbutton', 'position', [20 iter*30 50 20], 'string', 'Open...', 'callback', @(src,evt) disp( iter ))
end
should work. I don't really know what I would expect of 'iter' as a callback, but I guess you demonstrated what it does!

More Answers (1)

Jan
Jan on 14 Aug 2015
Edited: Jan on 14 Aug 2015
The callback 'iter' is evaluated when the button is pressed. This is a string and it displays the value of the variable 'iter' in the base workspace. I assume, that you want something else. Without knowing any details of what you want I guess boldly:
fucntion yourGuiCreator
for iter = 1:5
uicontrol('style', 'pushbutton', ...
'position', [20 iter*30 50 20], 'string', 'Open...', ...
'callback', {@myCallback, iter})
end
function myCallback(ObjectH, EventData, iter)
disp(iter)
Now iter is a variable and the current value inside the loop is stored statically in the callback's definition.
Note that I do not understand, what "iterating over buttons" mean. This loops create a set of buttons each time it is called.

Categories

Tags

Asked:

on 14 Aug 2015

Edited:

Jan
on 14 Aug 2015

Community Treasure Hunt

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

Start Hunting!