Search code examples
matlabmatlab-figure

turn on colorbar programmatically in clustergram


I know that one can insert a colorbar by clicking the colorbar icon in the clustergram GUI. Is there a way to do it programmatically? I tried

cgo = clustergram(data)
colorbar;

This makes a colorbar in a new figure window. How can a colorbar be created with proper positioning in a clustergram figure as if the button was clicked?


Solution

  • There is a function buried away (HeatMap.plot>showColorbar) that neatly positions the colorbar to the left of both the heat map and the dendogram (the lines). Just running colorbar(...) will mess up the relative positioning of the dendogram and the heatmap. So you need to somehow run the callback or carefully duplicate all of the position computations. It's easier to just run the callback. Here's how.

    To create the colorbar programmatically for a clustergram, and keep the color bar button in sync, you need to use the button's assigned callback and set the button's state.

    Create the clustergram:

    load filteredyeastdata
    cgo = clustergram(yeastvalues(1:30,:),'Standardize','Row');
    

    Get the handle for color bar button:

    cbButton = findall(gcf,'tag','HMInsertColorbar');
    

    Get callback (ClickedCallback) for the button:

    ccb = get(cbButton,'ClickedCallback')
    ccb = 
        @insertColorbarCB
        [1x1 clustergram]
    

    That gives us a handle to the function assigned by the callback (@insertColorbarCB), and the function's third input argument (the clustergram object). The button's handle and an empty event object are implicitly the first two arguments.

    Change the button state to 'on' (clicked down):

    set(cbButton,'State','on')
    

    Run the callback to create the colorbar:

    ccb{1}(cbButton,[],ccb{2})
    

    Note that the button State must be changed to 'on' first, otherwise the callback won't do anything.