Hello,
I have data which is extracted from various excel fiels in a folder. I want to sort the data based on the data in the first column. The issue is the data in this column is for ex, '10000 Rest', '3200 Rest', '2000 Rest', etc.. I need the data to be in ascending form. The output I receive is '10000 Rest', '2000 Rest', '3200 Rest', etc. The required output for me is '2000 Rest', '3200 Rest', '10000 Rest'.. etc.
Is there a way to change the sorting type.
Thanks

 Respuesta aceptada

Rishabh Mishra
Rishabh Mishra el 22 de En. de 2021
Editada: Rishabh Mishra el 22 de En. de 2021

0 votos

Hi,
Based on my understanding of the issue, I suggest you to implement the 'sort_nat' function. The function is used to sort array of strings in natural alphabetcial order. I have added a reference example code below to demonstrate the use of sort_nat function.
arr = {'10000 Rest','3200 Rest','2000 Rest'};
sort_nat(arr);
Hope this helps!

4 comentarios

Prathamesh Halagi
Prathamesh Halagi el 24 de En. de 2021
The problem for me with this is I have large data so doing this wont be very helpful.
clear
clc
ds_2 = spreadsheetDatastore('*dir', 'Sheets', 'sheet3', 'Range', 'A1:D20', 'FileExtensions', '.xls', 'ReadVariableNames', true);
ds_2 = readall(ds_2)
C = unique(ds_2)
[C,ia] = unique(ds_2.Var1);
B = ds_2(ia,:)
----
This is my code I have been using for now - I extract the data from multiple .xls files and then remove recurring data. I tried using natsort but it is not working I might be doing some mistake. I want to sort all the data based on the data in first column. I have 468x4 data.
Walter Roberson
Walter Roberson el 25 de En. de 2021
Editada: Walter Roberson el 25 de En. de 2021
Can the alphabetic part always be ignored for sorting purposes? For example, no "2000 Checkpoint" along with "2000 Rest" ?
[~, idx] = sort(arrayfun(@(S) sscanf(S, '%d', 1), ds_2.Var1));
Prathamesh Halagi
Prathamesh Halagi el 25 de En. de 2021
Yes, I think it can be ignored as all data has number followed by REST.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by