How to scroll a sorted UITable?

7 ビュー (過去 30 日間)
Gabriel Remi Vallat
Gabriel Remi Vallat 2022 年 6 月 10 日
回答済み: 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 Center および File ExchangeWhos についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by