Write certain lines of a text file to another text file

1 次查看(过去 30 天)
Hello All--
I have a problem that I could not solve it properly! I have a huge text file, and a single -column matrix. I have sorted this matrix in a ascending way. Each element of this matrix is corresponding to the line number of interest in the text file.
Basically I need to write a code such that the output file will include only the line of interest.
The following is the code I have written but it did not work at all! I do appreciate your helps.
Thanks
% I have tried to make a single-column excel file using the text file!!
filename1 = 'Text.xlsx';
Y = xlsread( filename1, 'A:A');
% This is the matrix containing the line numbers of interest
filename2 = 'Matrix.xlsx';
Z = xlsread( filename2, 'A:A');
Q=zeros(N,0); %N is size of the matrix Z
for i=1:N
Q(i,1) = Y((Z(i,1)),1);
end
xlswrite('newText.xlsx',Q ), % It did not work because my initial text file contains characters, numbers and words. it seems it cannot be transferred to a single-column matrix

回答(1 个)

Walter Roberson
Walter Roberson 2015-6-11
If you have a sufficiently new MATLAB:
readtable() to read in the data, index the table,
subset = TheTable(DesiredRows,:);
and then writetable() to create the new xlsx file.

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by