How do programatically sort a uitable?
42 views (last 30 days)
Show older comments
Is there a way to programatically sort a uitable? Basically as if the user had pressed the sort button on the column header? My issue is that I'm programatically changing the .Data on the table, and if the table has been sorted it reverts to its unsorted order and there's no way to put it back. 'DisplayData' stores the data in its sorted order, but its read only, and there's seems to be no way to programatically change it. Any ideas?
2 Comments
Mario Malic
on 9 Jan 2024
Maybe you could create a button with the callback to set the DisplayData to the Data property. In addition, you can also use UserData property for any reason, like saving the original data.
Answers (2)
Harimurali
on 10 Jan 2024
Hi Robert,
When the data in the "uitable" is changed programatically, instead of directly changing the data, the following can be done so that the data in the "uitable" is always sorted:
- Store the data in the "uitable" into a dummy variable
- Edit the data in the dummy variable
- Sort the data and store it back into the "uitable".
Here is an example MATLAB code to do the same (assuming "uit" is the "uitable" object:
newData = uit.Data;
newData{1,2} = 25; %changing the value in the second column in the first row
uit.Data = sortrows(newData, 2); %sorting based on the values in the second column
To provide more abstraction, a MATLAB function maybe defined to do the same:
function editTable(uit, row, col, val)
newData = uit.Data;
newData{row,col} = val;
uit.Data = sortrows(newData, col);
end
editTable(uit, 1, 2, 25)
The above code ensures that the data in the "uitable" is always sorted when the data in the table is changed programatically.
Callbacks maybe used to update the "uitable" automatically when some changes are made to the table via the table's user interface. Here is an example MATLAB code to do the same:
function uit = tableApp
% Create some sample data
data = {
'John Doe', 28, 'Male';
'Jane Smith', 34, 'Female';
'Emily Johnson', 40, 'Female';
'Michael Brown', 31, 'Male'
};
% Create UI figure
fig = uifigure;
% Create table UI component
uit = uitable(fig);
uit.Data = sortrows(data,2);
uit.ColumnSortable = false;
uit.ColumnEditable = [false true false];
sortColumn = 2;
uit.CellEditCallback = @(src,event) sortTable(src,sortColumn);
end
function sortTable(src,sortColumn)
sortedData = sortrows(src.Data, sortColumn);
src.Data = sortedData;
src.DisplayData
end
uit = tableApp
uit.Data{1,2} = 25
Refer the following Callbacks section of the given documentation for information about "uitable" callbacks:
Have a great day ahead.
0 Comments
Pratyush
on 10 Jan 2024
Hi Robert ,
I understand that you want to programatically sort a 'uitable'.
In MATLAB, there isn't a built-in function to programmatically sort a 'uitable' as if a user clicked the column header. However, you can achieve a similar effect by sorting the data in your workspace and then updating the 'uitable' with the sorted data.
You could do the following:
- Sort your data array or table based on the desired column using the 'sort' function for arrays or 'sortrows' for tables.
- Update the 'uitable' with the sorted data by setting the 'Data' property of the 'uitable'.
To maintain the sort order after updating the data, you'll need to:
- Store the last used sort criteria (column index and sort direction).
- Apply the stored sort criteria to the new data before updating the `uitable`.
This workaround requires you to manage sorting manually each time you update the table's data.
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!