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

for loopfscanfif statement

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

Best Answer

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