필터 지우기
필터 지우기

How to add uibuttongroup to a toolbar

조회 수: 4 (최근 30일)
xi
xi 2019년 10월 7일
댓글: xi 2019년 10월 10일
I want to add 3 toggle buttons to the toolbar. They are exclusive, i.e., only one of them should be pressed.
It is easy to create these buttons on a figure by using uibuttongroup, which takes care of the desired behavior. Is it possible to add them to the toolbar?

채택된 답변

Chidvi Modala
Chidvi Modala 2019년 10월 10일
Hello Xi,
From my understanding, you want to add 3 toggle buttons to the toolbar and you want only one toggle button being active at the same time.
You can make use of 'uitoggletool' to create a toggle button on toolbar and create appropriate callbacks for toggle action to set the state of other toggle buttons
For example
function createToggleButtons
f = figure('ToolBar','none');
tb = uitoolbar(f);
img = zeros(16,16,3);
% Create 2 toolbar items (set one among them to have ‘on’ State)
tb1 =uitoggletool(tb,'CData',img,'TooltipString','ToggleButton1','State','on');
tb2=uitoggletool(tb,'CData',img,'TooltipString','ToggleButton2');
% Set the callback for each toggle passing itself and the other toggle
% to the callback
set(tb1,'ClickedCallback',@(obj,event)ToggleToolbar(obj,tb2));
set(tb2,'ClickedCallback',@(obj,event)ToggleToolbar(obj,tb1));
end
function ToggleToolbar ( primary, secondary )
% Switch the other toolbar state based on the value of the
% toolbar which the user clicked on.
switch get ( primary, 'State' )
case 'on'
set ( secondary, 'State', 'off' );
case 'off'
set ( secondary, 'State', 'on' );
end
end
  댓글 수: 1
xi
xi 2019년 10월 10일
Although I'm seeking solutions by taking advantages of the "uibuttongroup", your solution is simpler than what I thought, especially the idea of using one shared callbackfunction.
If I have more than two toggle buttons, I can simply use "findobj" to set the states of all togglebuttons 'off' and turn on the current one.
Thx!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by