MatLab GUI:工具栏中的切换按钮(只有一个应该处于活动状态)

MatLab GUI: toggle buttons in toolbar (only one should be active)

我在 MatLab 中有一个 GUI,我希望其中有一些具有缩放功能的(切换)按钮。 我的问题是,我只想同时激活一个切换按钮: 1)我点击切换按钮A 2) 按钮 A 激活 3)现在我点击按钮B 4) 按钮 A 和 B 激活

但我想要的是,当我激活按钮 B 时,按钮 A 的状态变为 'off'。就像内置的 Matlab 绘图工具栏的行为一样。

这是我的按钮的代码:

%%% Zoom Toolbar
figureToolBar = uitoolbar;
% pointer button/all off
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_pointer.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uipushtool(figureToolBar,'Tooltip','Pan','CData',icon,...
        'ClickedCallback','zoom off; pan off;');
% pan button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_hand.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Pan','CData',icon,...
        'OnCallback','pan on','OffCallback','pan off');
% zoom in button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_zoom_in.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Zoom In','CData',icon,...
        'OnCallback','zoom on','OffCallback','zoom off');
% zoom out button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','matlab','icons',...
        'tool_zoom_out.png'));
    icon = double(img)/256/256;
    icon(~alpha) = NaN;
    % button
    uipushtool(figureToolBar,'Tooltip','Zoom Out','CData',icon,...
        'ClickedCallback','zoom out');
% zoom x button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','shared','sdi',...
        'web','MainView','release','SDI2','icons','toolstrip',...
        'ZoomInT_16.png'));
    icon = double(img)/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Zoom X','CData',icon,...
        'OnCallback','zoom xon','OffCallback','zoom off');
% zoom y button
    % icon
    [img,~,alpha] = imread(fullfile(matlabroot,'toolbox','shared','sdi',...
        'web','MainView','release','SDI2','icons','toolstrip',...
        'ZoomInY_16.png'));
    icon = double(img)/256;
    icon(~alpha) = NaN;
    % button
    uitoggletool(figureToolBar,'Tooltip','Zoom Y','CData',icon,...
        'OnCallback','zoom yon','OffCallback','zoom off');

如有必要,我可以使用 *.m 和 *.fig 文件给出一个最小的工作示例。

您需要使用切换动作的回调来设置其他动作的状态,例如:

function toggleTooolbarTest
  hTool = uitoolbar;
  % Create 2 toolbar items - setting one to have a state 1      
  tog1 = uitoggletool(hTool,'Tooltip','Toggle 1', 'CData', rand(16,16,3), 'State', 'on' );
  tog2 = uitoggletool(hTool,'Tooltip','Toggle 2', 'CData', rand(16,16,3) );

  % Set the callback for each toggle passing itself and the other toggle
  %  to the callback
  set(tog1,'ClickedCallback',@(obj,event)ToggleToolbar(obj,tog2));
  set(tog2,'ClickedCallback',@(obj,event)ToggleToolbar(obj,tog1));
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