Thanks for your answers. I ended up getting help from a friend and the result we ended up with was:
data=['C:\somewhere\somefile.txt];
FID=fopen(data,'rt');
lumberoflines = 100000;
for i=1:lumberoflines
l = fgetl(FID);
if strcmp(l(1),'$')
target = l(2:end);
l = fgetl(FID);
exist FID2;
if true(ans)
fclose(FID2)
end
FID2 = fopen(target,'w');
end
fprintf(FID2,[l '\n']);
end
close all
This solution saved the individual files with names corresponding to the header.
