How do I split a sing column .txt file by line?
Afficher commentaires plus anciens
Hey Guys,
How would I split a .txt file into smaller files by the number of lines? This was simple to do in linux, but I can't seem to do it here.
An example of a file is attached (testv2.txt)
EDIT: The .txt files I'm working with are very large, and I need to split them into files with 72,000,000 lines. I can't split the files by size, because for some reason some files are different sizes, and the script I'm using tells time by using the # of lines.
Thanks for the help guys!
4 commentaires
Adam Danz
le 28 Août 2019
After answering your question I realized that my answer only addresses half of your question. How would you like to split the data into subfiles? Are you sure you want to split up the data rather than keeping it all together?
EL
le 28 Août 2019
Adam Danz
le 28 Août 2019
What version of matlab are you using?
EL
le 28 Août 2019
Réponse acceptée
Plus de réponses (1)
This solution is quite fast and uses fgetl() to read in blocks of a text file and saves those blocks to a new text file. You can set the number of rows per block and other parameters at the top of the code. See comments within the code for more detail.
% Set the max number of lines per file. The last file may have less rows.
nLinesPerFile = 10000;
% Set the path where the files should be saved
newFilePath = 'C:\Users\name\Documents\MATLAB\datafolder';
% Set the base filename of each new file. They will be appended with a file number.
% For example, 'data' will become 'data_1.txt', 'data_2.txt' etc.
newFileName = 'data';
% Set the file that will be read (better to include the full path)
basefile = 'testv2.txt';
% Open file for reading
fid = fopen(basefile);
fnum = 0; % file number
done = false; %flag that ends while-loop.
while ~done
% Read in the next block; this assumes the data starts
% at row 1 of the txt file. If that is not the case,
% adapt this so that the header rows are skipped.
tempVec = nan(nLinesPerFile,1);
for i = 1:nLinesPerFile
nextline = fgetl(fid);
if nextline == -1
done = true;
tempVec(isnan(tempVec)) = [];
continue
else
tempVec(i) = str2double(nextline);
end
end
% Write the block to a new text file.
if ~isempty(tempVec)
fnum = fnum+1;
tempFilename = sprintf('%s_%d.txt',newFileName,fnum); % better to include a full
tempFile = fullfile(newFilePath,tempFilename);
fid0 = fopen(tempFile,'wt');
fprintf(fid0,'%.6f\n',tempVec);
fclose(fid0);
% (optional) display link to folder
disp(['<a href="matlab: winopen(''',newFilePath,''') ">',tempFilename,'</a>', ' saved.'])
end
end
fclose(fid);
5 commentaires
hamad javaid
le 8 Oct 2019
Dear Adam,
I have .txt files with 8 or 14 columns and continuous rows in thousands. i used this code to split the file into separate blocks of 2500 rows and columns and converted completely, but output files were created with NAN and only one column (NAN written 2500 times). As I have comma separated file.
Any suggestions please?
Adam Danz
le 8 Oct 2019
Hi Hamad,
I would use the debug feature.
Put a break point at the top of your code and step through each line, looking at the outputs. "tempVec " produces a vector of NaNs. Maybe those values are never being filled?
hamad javaid
le 15 Juin 2020
Dear Adam,
I have worked on this and it goes very well when tetx file has single column. I have attached my sample file when I use this code, it creates the subfiles but only one cloumn with NAN.Kindly guide me through this. As code is working fine. May b I am having trouble with comma '' ,'' as delimiter?
Thank you so much 

Adam Danz
le 15 Juin 2020
My answer pertains to the main question which asks about text files that have a single column of data.
In your case, check out readmatrix(). If you read the documentation for that function, you'll see optional inputs that specify what line number your numeric data start which will be useful in your case. Also check out readtable() for an alternative.
hamad javaid
le 17 Juin 2020
Thank you for your time
Catégories
En savoir plus sur Scripts dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!