Search code examples
matlabmatlab-guidetogglebutton

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


I have a GUI in MatLab in which I want to have some (toggle) buttons with zoom functions. My Problem is, that I want only one toggle button being active at the same time: 1) I click on toggle button A 2) Button A is active 3) Now I click on button B 4) Button A & B are active

But what I want is, that Button A's state goes to 'off' the moment I activate button B. Like the behaviour of the built in Matlab toolbars for plots.

Here is the code for my buttons:

%%% 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');

If necessary, I can give a minimal working example with the *.m and *.fig file.


Solution

  • You need use the callbacks for the toggle action to set the state of the others, for example something like this:

    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