Keep track of iterator in different pushbuttons
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (1)
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.
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!