How to create a variable number of checkboxes in GUI?

I'm trying to create a GUI with checkboxes, with the number of checkboxes being dependable on an input variable (here "stages"). In this case, "stages = 5", but "stages" will probably vary between 1-10 or so. I used a for-loop to create the desired number of checkboxes which are stored in a vector, but the problem is to assign each checkbox a specific function with "callback". With this code, checking different boxes results in the same action, that is: "A(5,1)=1". My wish is obviously to get "A(x,1)=1", where x = box number (or to have a complete different solution).
stages = 5;
fig = figure('Visible','off');
box = zeros(stages,1);
panel = uipanel('parent',fig,...
'Title','Choose stages to plot',...
'position',[.01 .05 .25 .95]);
A=zeros(stages,1);
for i = 1:stages
box(i,1) = uicontrol('parent',panel,'style','checkbox',...
'string',i,...
'position',[20 360-i*25 40 20],...
'callback','A(i,1)=1');
end
set(fig,'Visible','on')
Cheers

Réponses (1)

Callback should be
,'callback',@(src,evt)mycb(src,evt,i));
Now on each iteration of the for-loop a static snapshot of i is taken and the function mycb will receive it.
function mycb(src,evt,i)
A(i,1) = 1
% Do whatever with A
end
But now you have to worry about what happens to A after the function exits. What do you want to do with A?

2 commentaires

Daniel
Daniel le 1 Juil 2014
Modifié(e) : Daniel le 1 Juil 2014
I don't really understand exactly what you mean... Where should I put the code?
function mycb(src,evt,i)
A(i,1) = 1
% Do whatever with A
end
Well, the A is just for testing, but later a ticked/unticked box will mean plot/do not plot, and determine later actions in the program.
In that case I would not have the checkboxes do anything. When the later functions go to figure out what to plot, have them query the checkbox's 'Value' and make decisions then.

Connectez-vous pour commenter.

Catégories

En savoir plus sur App Building dans Centre d'aide et File Exchange

Question posée :

le 1 Juil 2014

Commenté :

le 1 Juil 2014

Community Treasure Hunt

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

Start Hunting!

Translated by