How can I select a whole row from a uitable when clicking on a cell in that row?

조회 수: 54 (최근 30일)
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
Abhishek Chakram 2022년 6월 8일
편집: Abhishek Chakram 2022년 6월 9일
Hi Radu,
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 :
function startupFcn(app)
app.UITable.SelectionType = 'row';
end
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.
  댓글 수: 1
Radu Andrei Matei
Radu Andrei Matei 2022년 6월 9일
This works, thank you. I don't really understand how to use the SelectionChangedFcn. You have to include it in some sort of callback with its handle right? But in what type of callback? I don't see any SelectionChanged callback when I go to UITable callbacks in R2020a

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Tom Teasdale
Tom Teasdale 2022년 6월 8일
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', ...
'CellSelectionCallback', @onCellSelection);
function onCellSelection(~, event)
indices = event.Indices;
disp(indices)
end
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;
  댓글 수: 1
Radu Andrei Matei
Radu Andrei Matei 2022년 6월 9일
Hi Tom, thanks for your answer. What I don't understand about it is that I've already created a table as an app designer component. If I do what you say I'd just create a new table with the right properties. But what I want is to change the properties of my already existing table. How do you think I could do that from within app designer? Thank you :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by