Domanda

I have a uitable in MATLAB and currently I have callback functions to every cell. I have been trying for a while now to set a callback to the column and row name, but with no success. More specifically if the user clicks on a particular column name, is it possible to call a function?

Please let me know if you need any more info... I would appreciate any help. Thanks in advance!

È stato utile?

Soluzione 2

This seems to be impossible using only the standard MATLAB interface to uitable.

However, you can access the underlying Java JTable as described on undocumentedmatlab.com. Using the JTable instance you should be able to install an appropriate event handler, see this question on SO and this other article on undocumentedmatlab.com.

Altri suggerimenti

First of all, you have to register callback to table header object. It is JTableHeader object, and you can access with findjobj function.

I created demo for registering callback function for column name click event. This callback function is used to modify the clicked column name. It is tested on Matlab R2015a.

To run this demo, download findjobj file, and put in the same folder. Then run below code.

function TableDemo()
% Demonstration of clickable columnname.
% In this example, we use the click event to modify column name.
figure('menubar','none','numbertitle','off', 'Name', 'DEMO');
myTable = uitable('Data', magic(4), 'ColumnName',{'A','B','C','D'}, 'ColumnWidth',{50});


% Accessing underlying java object.
jscrollpane = findjobj(myTable);
jtable = jscrollpane.getViewport.getView;
jheader= jtable.getTableHeader(); % Here, you got JTableHeader object.
h=handle(jheader, 'callbackproperties');

% Set a matlab function as MouseClickedCallback
set(h, 'MouseClickedCallback', {@onHeaderClick, jtable, myTable});

end

function onHeaderClick(src, evt, jtable, hTable)
if(get(evt, 'ClickCount') > 1)
    disp('header double clicked');

    % Get view index from current mouse point, and convert it to
    % model index. Then add 1 because Matlab index starts from 1.
    index = jtable.convertColumnIndexToModel(src.columnAtPoint(evt.getPoint())) + 1;

    prompt={'Column Name'};
    title='Enter column names';
    numLines=1;
    defaultAnswer=hTable.ColumnName(index);
    options.Resize='on';
    options.WindowStyle='modal';
    newName=inputdlg(prompt,title,numLines,defaultAnswer,options);
    if ~isempty(newName)
        hTable.ColumnName(index) = newName;
    end
end
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top