How to split data text file with equal elements in the row into several files?

Dear experts,
I have a doubt in a matlab code. I am not an expert in matlab and use it for matrix related options. I have never tried data mining with it. However, at the momentI want to split data text file with equal elements in the row into several files? The blocks of the text file that needs to be separated into several files are separated by a blank line. and I want to store the information in arrays.
I am trying to create a small script that writes all blocks in the original file into separate output files that contain only one block. Ideally these files would be named as the name as data1.txt data2.txt and so on where 1 and 2 are the number of blocks. Any help/suggestion is greatly appreciated.

 Accepted Answer

Try something like this
str = fileread('Try.txt');
parts = strsplit(str, '\n\r\n');
for i = 1:numel(parts)
filename = sprintf('data%d.txt', i);
fid = fopen(filename, 'w');
fprintf(fid, parts{i});
fclose(fid);
end

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!