How do I save data into a txt file from a xls file?

8 views (last 30 days)
question repost:- 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 .txt file . Similarly if number is "2" in 4th column then all rows which have 2 need to be sorted and written in a new txt file. How do i do that? P.S this new file being created must have all 19 columns in it. Thanks in advance

Answers (2)

Ingrid on 30 Nov 2015
Do you mean something like this? Since you did not attach a file, I could not do a test run so this code probably still contains errors but it should give you a general idea on how to proceed
formatString = repmat('%f',1,19);
fid = fopen('excelFile.xlsx','r');
rawData = cell2mat(textscan(fid,formatString));
% extract 4th column = 1;
dataWrite1 = rawData(abs(rawData(:,4)-1)<eps,:);
% write data to textfile
fid = fopen('textFileCreated.txt','w')

Walter Roberson
Walter Roberson on 30 Nov 2015
formatString = repmat('%.16g ',1,19);
formatString(end:end+1) = '\n';
data = xlsread('YourInputFile.xls');
[~, ~, group4] = unique(data(:,4));
numgroup = max(group4);
for G = 1 : numgroup
subset = data(group4==G, :);
thisfile = sprintf('YourOutputFile_part_%d.txt', G);
fid = fopen(thisfile, 'wt');
fprintf(fid, formatString, subset.' );

Community Treasure Hunt

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

Start Hunting!

Translated by