MATLAB: Splitting a table file into many files based on delimiter

split file

I have the following table file –
——————————
#
1.000000 NaN 0.000000
1.000000 90.000000 1.417979
1.000000 70.576875 3.007141
1.000000 3.513550 40.075328
1.000000 5.358103 40.175547
1.000000 3.428111 41.073496
#
2.000000 90.000000 1.417979
2.000000 NaN 0.000000
2.000000 67.845805 2.651813
2.000000 2.030255 40.025125
2.000000 3.513550 40.075328
2.000000 1.980806 41.024514
———————————
There are 500 such values. I wanted to split this file based on the delimiter # into 500 different files named file1.txt, file2.txt and so on.
Can someone please help me with this.

Best Answer

wholecontent = fileread('c:\pathtofile\filename.txt');
splitcontent = strsplit(wholecontent, '#'); %split content at each #
for fidx = 1:numel(splitcontent) %iterate over each section
fid = fopen(sprintf('c:\pathtodestination\file%03d.txt', fidx))
fwrite(fid, splitcontent{fidx}); %and save
fclose(fid);
end
Or something like that.