Using push button to run function based on checkboxes
18 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Suha lasassmeh
2018-6-14
编辑:Suha lasassmeh
2018-6-14
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)
0 个评论
Rik
2018-4-17
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!