Using push button to run function based on checkboxes

2 visualizzazioni (ultimi 30 giorni)
I have a GUI that has a few check boxes (cell diameter, cell size, and centroid) and when I click a push button, I want it to calculate the values of just the boxes that were checked. I have the coding for each of these check boxes in their call back, but when I run the GUI and 'check' the boxes, it runs those functions immediately. How can I get them to appear just when the boxes are checked and after I have clicked the push buttom?

Risposte (2)

Suha lasassmeh
Suha lasassmeh il 14 Giu 2018
Modificato: Suha lasassmeh il 14 Giu 2018
Don't write anything in the callbacks of the checkboxes. In the callback for your button write the code to execute the functions associated with each checkbox. See the example below:
% --- Execute on button press in PushButton
function PushButton_Callback(hObject, eventdata, handles)
% hObjet handle to PushButton
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cellDiameter = get(handles.cellDiametercheckbox,'Value');
cellSize = get(handles.cellSizecheckbox,'Value');
centroid = get(handles.centroidcheckbox, 'Value');
if cellDiameter
% write your function here for cell diameter
end
if cellSize
% write you function here for cell size
end
if centroid
% write your function here for centroid
end
% --- Execute on button press in cellDiametercheckbox.
function cellDiametercheckbox_Callback(hObject, eventdata, handles)
% hObject handle to cellDiametercheckbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Execute on button press in cellSizecheckbox.
function cellSizecheckbox_Callback(hObject, eventdata, handles)
% hObject handle to cellSizecheckbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Execute on button press in cellSizecheckbox.
function centroidcheckbox_Callback(hObject, eventdata, handles)
% hObject handle to centroidcheckbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Rik
Rik il 17 Apr 2018

Move the code to the callback for your button and include an if-statement around that code that queries the state (the Value property) of your checkbox.

Categorie

Scopri di più su Function Creation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by