How to click and sort a header in uiTable?

3 次查看(过去 30 天)
I would like to sort the columns in a table by clicking on the column header I want to sort.
Is there a way to click/select the headers and sort the column in uitable?

回答(1 个)

Rajanya
Rajanya 2024-11-26
As per my information, MATLAB's ‘uitable’ does not provide built-in support for sorting columns directly by clicking on the column headers.
However, there are a couple of ways in which a similar behaviour can be achieved.
a) You can use ‘uicontrol’ buttons (e.g. pushbutton) with the ‘ButtonDownFcn’ callback, above the column headers to each column, such that when the button above a particular column is pressed, the table gets sorted based on the cell values of that column. A sample function, called when any button is pressed, can look like:
data = rand(5, 3); % Random data for demonstration in a table with 3 columns
function sortTable(tableHandle, columnIndex)
% Get the table data
data_ = get(tableHandle, 'Data'); % Data is initialized to data
% Sort the data based on the selected column
[~, sortIdx] = sort(data_(:, columnIndex));
sortedData = data_(sortIdx, :);
% Update the table with sorted data
set(tableHandle, 'Data', sortedData);
end
After creating a table and adding the buttons with the callback, it sorts the data in the table based on the ‘column button’ pressed as shown:
Table originally:
Table after ‘Column 1’ is pressed:
b) You can use ‘CellSelectionCallback’ of ‘uitable’ in case of which, the table can get sorted based on the cell values belonging to the column of the selected cell (which is not the header to the column itself). A sample function here can look like this:
data = rand(5, 3); % Similarly, random data for demonstration
function sortTable(src, event)
% Get the table data and column index
data_ = get(src, 'Data'); %Data is initialized with data
colIndex = event.Indices(2);
% Similarly, sort the data based on the selected column
[~, sortIdx] = sort(data_(:, colIndex));
sortedData = data_(sortIdx, :);
% Update the table with sorted data
set(src, 'Data', sortedData);
end
In this case also, the table gets sorted based on the column of the selected cell, as shown:
Table gets sorted according to ‘Column3’:
To know more about ’uicontrol’, ‘uitable’ and the app callbacks, you can refer to their respective documentation pages by executing the following commands from the MATLAB Command Window:
doc uicontrol
doc uitable
doc CellSelectionCallback
doc ButtonDownFcn
Thanks.
  2 个评论
sfreeman
sfreeman 2025-1-31
Rajanya,
while this question is outdated, your answer is not correct.
The newer uitable based on uifigure has builtin sorting - the old table GUI element was more limited.
An example is:
of = uifigure;
ot = uitable(of);
ot.Data = rand(3);
ot.ColumnSortable = true(1,3);
Error using matlab.internal.capability.Capability.require (line 94)
This functionality is not available on remote platforms.

Error in matlab.ui.internal.uifigureImpl (line 35)
Capability.require(Capability.WebWindow);

Error in uifigure (line 34)
window = matlab.ui.internal.uifigureImpl(varargin{:});
column 2 is sorted, column3 shows the sort icon during a mouse over
Rajanya
Rajanya 2025-1-31
Keeping in mind the version of the question, property 'ColumnSortable' was not added back in versions like MATLAB R2018b. Therefore, the workarounds using 'ButtonDownFcn' and 'CellSelectionCallback' were provided as they would be working in all cases.
However for the later releases, as you suggested, one can directly leverage the 'ColumnSortable' property to sort the columns by clicking on the headers. Thanks a lot for pointing this out!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by