Search code examples
matlabuser-interfacematlab-figure

How to change the button icons on the MATLAB figure toolbar? (2014b)


I'm writing an application in MATLAB and want to update the look of it. is it possible to change the icons of the buttons in the toolbar in MATLAB code?

The code will be compiled and I am not using GUIDE; ideally there's a way to get the button handles and set each icon individually although I don't know how I would do this.

Example of what I mean

But with higher quality icons.


Solution

  • Yes, you can change the figure toolbar icons, or add your own.

    I've detailed how to change the icon below, as well as other useful things to do with the toolbar whilst you're editing the properties anyway.

    See the code comments for details.

    Get the toolbar object

    % Initialise some figure 
    fig = figure( 'Name', 'MyApp', 'NumberTitle', 'off' )
    % Get the figure toolbar handle
    tbar = findall( fig, 'tag', 'FigureToolBar' );
    

    You can do findall(tbar) at this point to list the names of all the buttons

    Hiding buttons

    Let's say you want to hide the "new figure" button:

    % Get the button from the tbar object
    btn = findall( tbar, 'tag', 'Standard.NewFigure' )
    % Set to not visible
    set( btn, 'Visible', 'off' );
    

    Changing callbacks

    Let's say you want the print button to trigger a print-preview callback instead of printing directly (you could assign any custom callback function to any button):

    % Get the button again
    btn = findall( tbar, 'tag', 'Standard.PrintFigure' );
    % Change the callback (and the tooltip to match)
    set( btn, 'ClickedCallback', 'printpreview(gcbf)', ...
              'TooltipString', 'Print preview' );
    

    Changing the icon

    At this point you can see all button attributes are editable, including the image as per your original question.

    In particular, just change the CData property:

    % Update the print button to have a print preview icon
    % This should be a 16*16 RBG image matrix, use imread to get from file
    img = imread( 'printpreview.bmp' )
    % Assign CData property to button already acquired
    set( btn, 'CData', img );
    

    Output (I used a random print preview icon, seen 4th from the left):

    figure

    Add new buttons

    You can add new buttons by simply creating uipushtool objects (with CData property set for the icon image) with the tbar object as a parent.

    Change the separators

    The vertical grey separators can be added or removed (useful for creating your own button groups or when removing buttons). Simply set the 'Separator' property to 'off' or 'on', for a separator on the left side of a button.


    For a compiled app, this may be against The MathWorks T&Cs, but this is the how to not the should you do this!