Search code examples
matlab

Make all columns narrower in Matlab Variables editor


I often use openvar('myArrayName') to browse my data. However, I got spoiled by Excel's ability to narrow all columns at once, allowing me to see lots of data. It's very stifling to use the limited bit of desktop space that I have to peer at so few columns at once. Manually adjusting each column is not a good solution because I'm constantly opening up variables, and they often have many columns. Is there a way to narrow the columns in a bulk manner?


Solution

  • This is not a perfect solution, but can get you started. The basic idea is to first get a handle to underlying java table, and then resize all columns, e.g. as suggested here. You can download TableColumnAdjuster java class, package it into a jar and load using javaclasspath. When searching for a java handle using findjobj, note that table class will depend on the type of the variable you are trying to display (some examples here), so we need to find the right handle.

    javaaddpath('path\to\TableColumnAdjuster.jar');
    my_var= randn(1e4,10);
    openvar('my_var');
    resizevar('my_var');
    
    function resizevar(name)
        desktop = com.mathworks.mde.desk.MLDesktop.getInstance();
        varclient = desktop.getClient(name);
        jh = findjobj(varclient);
        hNames = get(jh, 'Name');
        validNames = {'VariableTable','CellTable','DatasetVariableTable','TableObjectVariableTable','CategoricalVariableTable','TimeSeriesArrayEditorTablePanel:fDataTable'};
        jTable = jh(find(ismember(hNames, validNames),1));
    
        jTable.setAutoGrowthTemporarilyDisabled(true);
        jTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        pause(0.1);
    
        spacing = 10;
        tca = TableColumnAdjuster(jTable, spacing);
        tca.adjustColumns();
    end
    

    The problem is that Matlab will keep updating the table as you scroll and/or underlying data changes, which may reset the width (or even have other side-effects like adding more empty columns etc). If you explore the custom table class used by Matlab, you will notice a handy setAutoGrowthTemporarilyDisabled method. Setting that to true allows us to temporarily avoid these side-effects (pause seems to be required for it to take effect), however columns may still be resized once you start interacting with table/data. You would need to explore this custom table class further to see how this can be prevented.