Write certain lines of a text file to another text file

3 views (last 30 days)
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

Answers (1)

Walter Roberson
Walter Roberson on 11 Jun 2015
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.

Community Treasure Hunt

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

Start Hunting!