How to write it into a txt file instead of a xls file?
how to read a .xls file , sort data based on one of the columns and write the sorted data into separate .xls files
2 views (last 30 days)
Show older comments
Srinidhi B.R
on 29 Nov 2015
Commented: Walter Roberson
on 24 Jun 2022
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 Comments
Nkululeko Memela
on 4 Nov 2018
Edited: Nkululeko Memela
on 4 Nov 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.
Accepted Answer
Walter Roberson
on 29 Nov 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 Comments
John Allen
on 24 Jun 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.
Walter Roberson
on 24 Jun 2022
1 a : to put in a certain place or rank according to kind, class, or nature
sort apples
sort mail
More Answers (0)
See Also
Categories
Find more on Spreadsheets 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!