How to scroll a sorted UITable?

7 次查看(过去 30 天)
Gabriel Remi Vallat
回答: Shivam 2024-1-5
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 个评论
dpb
dpb 2022-6-10
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.

请先登录,再进行评论。

回答(1 个)

Shivam
Shivam 2024-1-5
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

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by