How can I select a whole row from a uitable when clicking on a cell in that row?
43 views (last 30 days)
Radu Andrei Matei on 8 Jun 2022
Hi, I have a table in my app designer app that displays results. What I want to do is to have a whole row selected when any cell of that row is clicked on, to be able to do that with multiple row when they are selected, and to make the app do stuff with the information in those rows (but this is secondary). I've found in the documentation that you can do this with the SelectionType and Multiselect properties of the table but I can't figure out how to set those properties to 'row' and 'on' respectively as I don't find a way to create a Selection changed callback. Any help will be appreciated.
Abhishek Chakram on 8 Jun 2022
Edited: Abhishek Chakram on 9 Jun 2022
As I understood, you want the entire row to be selected when the user click on any cell of that row.
To this, you can create a startup function that is automatically called when the app starts and write this in it :
app.UITable.SelectionType = 'row';
To create a startup function, go to Component Browser, Select app and add startupFcn through callbacks.
Futher, you can use the SelectionChangedFcn of the UITable to detect the selection.
More Answers (1)
Tom Teasdale on 8 Jun 2022
Note that the properties you are trying to use seem to have been added officially in MATLAB R2022a. However, the following still works for me in R2020b. Here is a full example of printing the selected table indices to the console, with Multiselect enabled and SelectionType set to row.
n = 4;
uit = uitable(uifigure(), 'Data', reshape(1:n^2,n,n), ...
'Multiselect', 'on', ...
'SelectionType', 'row', ...
function onCellSelection(~, event)
indices = event.Indices;
In case you want to select a row programatically, try the following. If you don't know n beforehand, count the number of colums the Data property of your table has.
rowToSelect = 2;
uit.Selection = rowToSelect;