How do programatically sort a uitable?

39 visualizzazioni (ultimi 30 giorni)
Robert Aungst
Robert Aungst il 9 Gen 2024
Commentato: Robert Aungst il 10 Gen 2024
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 Commenti
Mario Malic
Mario Malic il 9 Gen 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.
Robert Aungst
Robert Aungst il 9 Gen 2024
Changing Data is an option, but that defeats the purpose of having a DisplayData attribute. I'd like to leave Data alone as its referenced by other things and changing the order causes all sorts of other problems. I'm trying to programatically change Data, and have DisplayData update automatically based on how the user has sorted or rearranged things.

Accedi per commentare.

Risposte (2)

Harimurali
Harimurali il 10 Gen 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.

Pratyush
Pratyush il 10 Gen 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:
  1. Sort your data array or table based on the desired column using the 'sort' function for arrays or 'sortrows' for tables.
  2. 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:
  1. Store the last used sort criteria (column index and sort direction).
  2. 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.
  1 Commento
Robert Aungst
Robert Aungst il 10 Gen 2024
Thanks! I was hoping there was some workaround to trigger the callback so that 'DisplayData' would update properly. The beauty of it is that you don't have to keep track of the sorted indices and .Selection updates correctly. Hopefully this is a feature added in the future.

Accedi per commentare.

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by