Search code examples
matlabuitableviewindices

UITable Matlab - Accessing specific cell


I am getting the row index of the selected cell with this code:

row = eventdata.Indices(1);

I can also get the column index by changing the 1 to 2. But I want to be able to get the content of any cell I want in that specific row without the user having to actually click on that specific cell but rather anywhere in that row. Let's say I want to get the data from the first column which in my case represents the ID.

In pseudo code it would look like:

x = getRowOfSelectedCell
field = Indices(x,1);

Say the selected row is 5. The variable field would consist of the value of the cell in the first column in row 5.

Any ideas how to proceed?


Solution

  • What about:

    function ScriptTest
    
    d = rand(10,7);
    t = uitable('Data', d,  'Units', 'norm', 'Position', [0,0,1,1]);
    
    RequiredColumnIndex = 5;
    set( t, 'CellSelectionCallback', {@TableSelectionCB, RequiredColumnIndex});
    
    function TableSelectionCB(hTable, eventdata, RequiredColumnIndex)
        rowIndex = eventdata.Indices(1);
        TableData = get(hTable,'Data');
    
        field = TableData(rowIndex, RequiredColumnIndex);
        fprintf(' Value in cell [Row %d /Col %d ] is %f \n',  rowIndex, RequiredColumnIndex, field);
    end
    
    end
    

    Here I decided to retrieve the data in column 5 (as you suggested) and printed the corresponding cell value in the command window.