MATLAB: How to get row number selected row in uitable GUI

checkboxesgui

In a GUI I have a uitable made of multiple rows and 3 columns. The third column contains checkboxes. When I select a certain row's checkbox I want to get the number of that row (if I selected nth row, I want n) and store it as a variable. How can I do this?

Best Answer

syh1234 - since you are making a change to uitable (i.e. selecting or de-selecting the checkbox), then you can use the CellEditCallback which will fire every time that the table is edited. Depending upon how you are building your GUI (using GUIDE or programmatically) then you should be able to determine which row was updated and save that value for future use. For example, if using GUIDE, then the callback would be
function uitable1_CellEditCallback(hObject, eventdata, handles)
selectedRow = eventdata.Indices(1);
selectedCol = eventdata.Indices(2);
if selectedCol == 2
if eventdata.NewData == 1
handles.selectedRow = selectedRow;
else
handles.selectedRow = [];
end
guidata(hObject, handles);
end
First we determine the selected row and column in the table. If the second column (which in my example corresponds to a column of check boxes) then we set the selectedRow field on the handles structure with this row if selected (and so the NewData field is 1). If de-selected, then the selectedRow is set to an empty value. Finally, guidata is used to save the updated handles structure so that the other callbacks have access to the selected row.