How to scroll a sorted UITable?

7 views (last 30 days)
Gabriel Remi Vallat
Gabriel Remi Vallat on 10 Jun 2022
Answered: Shivam on 5 Jan 2024
I need to programmaticaly scroll to a selected line of a UITable in an app, and everything works perfectly until I use the "ColumnSortable" option of the UITable: The scroll function goes to the original location of the line, and not at it sorted location. Do you have any workaround for such cases?
  1 Comment
dpb
dpb on 10 Jun 2022
Not directly, but I'd guess you'd probably have to rewrite the .Data property with the data in the sorted order and turn of 'Sort' flag.

Sign in to comment.

Answers (1)

Shivam
Shivam on 5 Jan 2024
Hi,
From the provided details, I understand that after using the 'ColumnSortable' property of the uitable, the scroll function goes to the original row, not the sorted row of the uitable.
It is important to note that when you set the 'ColumnSortable' property of a uitable in MATLAB to true, you can click on the column headers to sort the table by that column. However, this sorting is only a visual change and does not modify the underlying data array in the 'Data' property of the uitable. That is why the 'scroll' function does not reach the sorted location.
To overcome this issue, you can sort the original matrix and set the Data property of uitable to the sorted matrix.
You can follow the below workaround to sort the original matrix and set the Data property:
% Create data for uitable
mat = randi(30,10,2)
% Create uitable
fig = uifigure;
uit = uitable(fig,"Data",mat);
col = ["A","B"];
uit.ColumnName = col
% Sort original matrix using column 1 and set uit.Data to sorted matrix
sortedMat = sortrows(mat, 1)
uit.Data = sortedMat
You can now use the 'scroll' function to navigate to the correct sorted location.
Please refer to the following documentation to know more about 'uitable', 'scroll' and 'sortrows' functions:
I hope it helps in resolving the issue.
Thanks

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!