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

时间:2015-04-16 06:40:25

标签: matlab matlab-guide togglebutton

我在MatLab中有一个GUI,我想要一些具有缩放功能的(切换)按钮。 我的问题是,我只希望同时有一个切换按钮处于活动状态: 1)我点击切换按钮A. 2)按钮A有效 3)现在我点击按钮B. 4)按钮A& B有效

但我想要的是,按钮A的状态是“关闭”。我激活按钮B的那一刻。就像内置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文件给出一个最小的工作示例。

1 个答案:

答案 0 :(得分:2)

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

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