How to scroll a sorted UITable?
7 次查看(过去 30 天)
显示 更早的评论
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
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
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:
- https://www.mathworks.com/help/releases/R2023b/matlab/ref/uitable.html
- https://www.mathworks.com/help/releases/R2023b/matlab/ref/matlab.ui.container.tree.scroll.html
- https://www.mathworks.com/help/releases/R2023b/matlab/ref/double.sortrows.html
I hope it helps in resolving the issue.
Thanks
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!