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

3 次查看(过去 30 天)
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

回答(2 个)

Ingrid
Ingrid 2015-11-30
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));
fclose(fid);
% extract 4th column = 1;
dataWrite1 = rawData(abs(rawData(:,4)-1)<eps,:);
% write data to textfile
fid = fopen('textFileCreated.txt','w')
fprintf(fid,formatString,dataWrite1)
fclose(fid);

Walter Roberson
Walter Roberson 2015-11-30
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.' );
fclose(fid)
end

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by