how to read a .xls file , sort data based on one of the columns and write the sorted data into separate .xls files

1 visualización (últimos 30 días)
I have an excel data file (only floating point numbers no strings) with 19 columns and 11k rows or so. I want to sort this data based on a particular column, i.e, if the number in 4th column (of say 5th row) is "1" then, all the rows which have 4th column as 1 need to be separated out and written into a new .xls file . Similarly if number is "2" in 4th column then all rows which have 2 need to be sorted and written in a new xls file. How do i do that? P.S this new file being created must have all 19 columns in it. Thanks in advance
  2 comentarios
Nkululeko Memela
Nkululeko Memela el 4 de Nov. de 2018
Editada: Nkululeko Memela el 4 de Nov. de 2018
I find this Solution very helpful as well. I only wish that it had comments on each step because I am trying to use the idea in it to solve my specific problem. My problem if I could be lucky to receive any help as well is this: My Spreadsheet of 60 columns by 43000 rows has time and date variables. I would like to get the new spread sheet with data sorted by date and time whom are column 16 and 20 respectively. The date is repeated over many lines 2-8 as long as there is multiple times of a specific event in that day. I.e. I have (2) 8am times, (2) 11am and (4) 3pms time. I intend to sort my data in the new spreadsheet by unique times in a day. The whole spread sheet must be retained and it contains both strings and numerals.
Thank you in advance.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Nov. de 2015
data = xlsread('YourInputFile.xls');
[~, ~, group4] = unique(data(:,4));
numgroup = max(group4);
for G = 1 : numgroup
subset = data(group4==G, :);
thisfile = sprintf('YourOutputFile_part_%d.xls', G);
xlswrite(thisfile, subset);
end
  3 comentarios
John Allen
John Allen el 24 de Jun. de 2022
Sorry to be picky, but to my mind this is filtering and not sorting. Sorting would involve rearranging rows based on the value in one or more columns. :-)
If you want to sort then sortrows() is probably what you're want to check out.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by