이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi i have GUI with 18 checkboxes/radiobuttons. Dont know what is better for me...Is there way to set that only two of them can be checked at the same time?? Thanks.
채택된 답변
Walter Roberson
2013년 4월 12일
Only by using the callbacks to check and enforce it.
댓글 수: 9
Jakub
2013년 4월 12일
And if i use radiobuttons instead of checkboxes? is there any difference?
No difference. radiobuttons can be managed by a uibuttongroup() but uibuttongroup() allows either 0 or 1 item to be selected.
Jakub
2013년 4월 12일
Ok, thanks. Can you give me a siplme example please. How to check how many checkboxes are checked. and if two are checked, uncheck one and check another which i clicked on.
If you don't use a button group you can have each checkbox or radio button's callback call the same function, let's say it's called "CountSelectedStates(handles)". Then in that function, you check the state of each checkbox or radio button. The trouble comes in when you have three selected. You're going to have to remember the last state, say an 1 by 18 array of booleans, and figure out which of the two "old" states should be turned off now that the user has selected a third button or box. So if two are selected, and they select a third, how do you decide which one of the two to turn off?
If you use something like
function CountSelectedStates(hobject, event, handles)
then hobject will be the handle of the box that was just checked or un-checked; by get(hobject, 'Value') you can figure out if it was just turned on; if it was, then you could set() it off again. In this way you do not need to remember the order of the boxes being checked, you just refuse to let the third one be checked.
Or another approach, using the same calling sequence, would be when the second box was checked, to set() all of the "off" boxes to 'enable', 'off' so that no more can be checked until one of the two active ones is unchecked (at which point you would re-enable all of them.)
Jakub
2013년 4월 14일
Thank you for advices, i think ill do that when third chechbox is going to be checked, the previous two will be unchecked.
Jakub
2013년 4월 14일
I dont know how to deal with the function to count selected states...I would appreciate a help, thank you.
function CountSelectedStates(hobject, event, handles)
% Get states of all the checkboxes.
selectedStates(1) = get(handles.checkbox1, 'value');
selectedStates(2) = get(handles.checkbox2, 'value');
selectedStates(3) = get(handles.checkbox3, 'value');
selectedStates(4) = get(handles.checkbox4, 'value');
% Count how many are checked right now.
totalNumberSelected = sum(selectedStates);
% Check which one was just toggled
if hObjects = handles.checkbox1
% If you want to implement Walter's suggestion.
% They just clicked on checkbox 1.
% more code.....
end
if totalNumberSelected >= 3
% Clear states of all the checkboxes, like you wanted.
selectedStates(1) = get(handles.checkbox1, 'value');
selectedStates(2) = get(handles.checkbox2, 'value');
selectedStates(3) = get(handles.checkbox3, 'value');
selectedStates(4) = get(handles.checkbox4, 'value');
% Keep the one they clicked on checked.
set(hObject, 'value', 'on');
end
and so on.
Some small corrections to the above:
if hobject == handles.checkbox1
and to clear the check boxes, do not get() the values, instead
set(handles.checkbox1, 'value', 0);
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 App Building에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
