Table resorting after deleting selected rows from the sorted Table

I have a table data with gender and age.
First I will sort in descending based on age and I will select above 60 age of peoples and delete it.
Now I want to see the remaining data with same sorting(descending order based on age) on the table .
Here, The table property not able to set as Descending order sort through code.
Could you please help us for this issue?

Answers (1)

You haven't shared the code you are using to complete your tasks. That will allow us to say why it is happening.
However, here is code I would use to sort then delete rows from a table. As you can see, the final table is still sorted in desceding age order.
T = readtable('patients.xls');
T = sortrows(T,"Age",'descend')
T = 100x10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus ____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ {'Robinson'} {'Male' } 50 {'County General Hospital' } 68 172 false 125 76 {'Good' } {'Reed' } {'Male' } 50 {'VA Hospital' } 72 186 true 129 89 {'Excellent'} {'Brown' } {'Female'} 49 {'County General Hospital' } 64 119 false 122 80 {'Good' } {'Stewart' } {'Male' } 49 {'County General Hospital' } 68 170 true 129 95 {'Poor' } {'Hughes' } {'Female'} 49 {'County General Hospital' } 63 123 true 128 96 {'Good' } {'Griffin' } {'Male' } 49 {'County General Hospital' } 70 186 false 119 74 {'Fair' } {'Martin' } {'Male' } 48 {'VA Hospital' } 71 181 true 130 92 {'Good' } {'Clark' } {'Female'} 48 {'VA Hospital' } 65 133 false 121 75 {'Excellent'} {'Adams' } {'Female'} 48 {'VA Hospital' } 66 137 false 127 83 {'Excellent'} {'Ramirez' } {'Female'} 48 {'County General Hospital' } 64 137 true 138 82 {'Excellent'} {'Ross' } {'Female'} 48 {'VA Hospital' } 64 126 false 118 79 {'Good' } {'Gonzales'} {'Male' } 48 {'County General Hospital' } 71 174 false 123 79 {'Good' } {'Bryant' } {'Female'} 48 {'County General Hospital' } 66 134 false 129 73 {'Excellent'} {'Hayes' } {'Male' } 48 {'County General Hospital' } 66 177 false 114 86 {'Fair' } {'Scott' } {'Male' } 47 {'St. Mary's Medical Center'} 70 187 false 127 84 {'Excellent'} {'Rogers' } {'Female'} 47 {'VA Hospital' } 66 147 false 117 86 {'Excellent'}
T(T.Age>45,:)=[]
T = 83x10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus ____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ {'Anderson'} {'Female'} 45 {'County General Hospital' } 68 128 false 114 77 {'Excellent'} {'Wright' } {'Female'} 45 {'VA Hospital' } 70 126 true 134 92 {'Excellent'} {'Phillips'} {'Male' } 45 {'VA Hospital' } 67 172 false 117 89 {'Good' } {'Bell' } {'Male' } 45 {'St. Mary's Medical Center'} 70 170 true 138 82 {'Good' } {'Torres' } {'Female'} 45 {'County General Hospital' } 70 137 false 119 79 {'Excellent'} {'Simmons' } {'Male' } 45 {'VA Hospital' } 71 181 false 124 77 {'Excellent'} {'Diaz' } {'Male' } 45 {'County General Hospital' } 68 172 true 136 93 {'Good' } {'Lee' } {'Female'} 44 {'County General Hospital' } 66 146 true 128 90 {'Fair' } {'Green' } {'Male' } 44 {'County General Hospital' } 71 193 false 121 92 {'Good' } {'Baker' } {'Male' } 44 {'VA Hospital' } 71 192 true 136 90 {'Good' } {'Perez' } {'Male' } 44 {'VA Hospital' } 69 183 false 116 80 {'Excellent'} {'Roberts' } {'Male' } 44 {'VA Hospital' } 70 169 true 132 89 {'Good' } {'Sanchez' } {'Female'} 44 {'St. Mary's Medical Center'} 62 136 true 130 91 {'Good' } {'Russell' } {'Male' } 44 {'VA Hospital' } 69 188 true 124 92 {'Good' } {'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' } {'Morris' } {'Female'} 43 {'County General Hospital' } 64 135 true 132 91 {'Poor' }

6 Comments

Thank you very much for your effort.
I'm working on MATLAB App Designer to develop this application. I can able to do sorting in coding level.
How to control this sorting option(marked in red) on the table through coding?
Here, I have attached .mlapp for your reference.
Instead of the whole app, just pull out the specific code for the table and paste it as text (remembering to format as code) so folks can see here what you did...
I don't know if it is possible to programmatically sort a uitable this way. It appears you need to first sort the data then add it to the uitable.
Note that sorting a uitable only changes how the table is displayed. It does not actually sort the table data.
You could simplify the delete at least
% Key press function: UITable
function UITableKeyPress(app, event)
key = event.Key;
selection = app.UITable.Selection;
if ~isempty(selection) && strcmp(key, 'delete')
app.UITable.Data(selection,:) = [];
end
end
@Cris LaPierre thanks for your recommandation.
My expectation is very specific,
I can able do sorting as you said.
but, after reloading sorted data on the Table, have to show the data in which sorting method applied and which column attributes based sorting is done on the table. So, I have to show the Age Column and down Arrow to represent the "Descending order" by the code without manual intervention.
Thanks for simplified delete option.
I understand what you want. I just don't know that there is a documented approach that achieves it programmatically.
Rather than use the column headers to sort the table, you could implement a programmatic sort of the data with app buttons and custom callbacks. This will meet your design requirements, though the arrow still won't appear. Instead, a dropdown menu where you selected the sort order would indicate it.
Here's I've implemented a solution and removed all those who are 48.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 23 Apr 2025

Commented:

on 24 Apr 2025

Community Treasure Hunt

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

Start Hunting!