uicontrol used to create a checkbox, how to program this checkbox via Guide.... the Callback for checkbox is not in the M file

20 views (last 30 days)
I was able to create a set of checkboxes using uicontrol....I need 40 checkboxes and thought it would be faster to use some code and get this done....great it worked....
cbh(i)....checkbox (1)to checkbox(40)....assigning a string from handles.label(i)....this works great
for i=1:40; cbh(i) = uicontrol('Style','checkbox',... 'String',handles.label(i),... 'Value',0,'Position',[30 20*i 130 20]);
end
Problem Statement: How do I know when a checkbox has been selected. A Callback routine has not been created in the m file for each checkbox when using the uicode method.
I understand I can use Guide and manually insert 40 checkboxes , which will then have the 40 Callback routines I am require. I will do this if required, if I can get a solution with the uicontrol method that would be ideal
I need to know that a Checkbox state has changed and then update a plot.
thanks gb/

Accepted Answer

Geoff Hayes
Geoff Hayes on 9 Oct 2014
Gary - rather than creating 40 callbacks, you could just use the same one for each of the 40 checkboxes. Try the following
for k=1:40;
cbh(k) = uicontrol('Style','checkbox','String',handles.label(k), ...
'Value',0,'Position',[30 20*k 130 20], ...
'Callback',{@checkBoxCallback,k});
end
For each checkbox, we assign the callback @checkBoxCallback. We wrap it in braces so that we can include additional input parameters to the function. In this case, k, the id of the checkbox. We can now define the callback as
function checkBoxCallback(hObject,eventData,checkBoxId)
value = get(hObject,'Value');
if value
switch checkBoxId
case 1
fprintf('handle cb 1\n');
case 2
fprintf('handle cb 2\n');
otherwise
fprintf('do nothing\n');
end
end
The function signature is
function checkBoxCallback(hObject,eventData,checkBoxId)
with the expected inputs of hObject, the handle to the checkbox that has been checked or unchecked; eventData, an empty matrix of nothing (this is expected); and checkBoxId, which is the k from our for loop. In this example, we get the value of the checkbox (checked or not), and if it is, we evaluate some code.
Now, if you have added this code into your GUI that you have developed with GUIDE (which seems to be the case given the handles structure that you reference to get the labels from) then presumably you are running this code in the OpeningFcn of your GUI m file. If that is the case, then you may want to set the parent of the checkbox to be the figure. That way, in the callback, you can make use of guidata. As well, you may want to save your list of checkbox handles to the handles structure (just for future reference, and because all widget/control handles are saved to this structure).
handles.cbh = zeros(40,1);
for k=1:40;
handles.cbh(k) = uicontrol('Style','checkbox','String',handles.label(k), ...
'Value',0,'Position',[30 20*k 130 20], ...
'Parent', hObject, ...
'Callback',{@checkBoxCallback,k});
end
guidata(hObject,handles);
Then in the callback, you can grab the handles structure to reference any other controls (axes for plotting, etc.) or user-defined data.
function checkBoxCallback(hObject,eventData,checkBoxId)
handles = guidata(handles);
value = get(hObject,'Value');
% etc.
Try the above and see what happens!
  2 Comments
Gary
Gary on 9 Oct 2014
Thanks Geoff!! once again, immediate solution and leads to increase my knowledge....your guide helped me dig deeper on Structures and how to use it within the code on my present design.
gary

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!