2015-04-08 62 views
0

我得到這個代碼選定單元格的行索引:UITable Matlab的 - 訪問特定的細胞

row = eventdata.Indices(1); 

我還可以通過改變1〜2得到列索引,但我希望能夠在特定行中獲取我想要的任何單元格的內容,而用戶不必實際單擊該特定單元格,而是在該行中的任何位置。比方說,我想從第一列獲取數據,在我的案例中代表ID。

在僞代碼它看起來像:

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

假設所選擇的行是5的可變場將在第5行

任何由在第一列中的單元格的值的想法如何進行?

回答

1

什麼:

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 

在這裏,我決定檢索5列中的數據(如你所說),並印在命令窗口中的相應單元格值。

+0

謝謝,它爲我工作:) –