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

조회 수: 3 (최근 30일)
Srinidhi B.R
Srinidhi B.R 2015년 11월 30일
답변: Walter Roberson 2015년 11월 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에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by