How to speed up saving data to .txt file?

17 views (last 30 days)
AStro
AStro on 2 Sep 2021
Edited: Jan on 26 Sep 2021
Hi,
I am working on large data - several hundered waveforems, each with sampling rate of 10e7. My goal is to save data in to the txt file. Currently I am using 'writematrix' and 'append' for including all the information I need. The part of my code looks as follow:
for i = 1:length(Signal)
writematrix('Sampling Rate [Hz]:',SaveFile,'WriteMode','append');
writematrix(SampRate,SaveFile,'WriteMode','append');
writematrix('Test Time [sec]:',SaveFile,'WriteMode','append');
writematrix(TestTime(i),SaveFile,'WriteMode','append');
writematrix('Amplitude [mV]:',SaveFile,'WriteMode','append');
writematrix(Signal{i},SaveFile,'Delimiter','space','WriteMode','append');
writematrix('',SaveFile,'WriteMode','append');
end
As for now, the procoess of saving the data take forever. Is there any way to speed it up?

Accepted Answer

Jan
Jan on 2 Sep 2021
Edited: Jan on 26 Sep 2021
fopen, fprintf or even better fwrite is much faster than the very smart writematrix. Appending requires to open the file and searching the end. It is much faster to open the file once only and write the data directly.
fid = open(Savefile, 'W');
for i = 1:length(Signal)
fprintf(fid, 'Sampling Rate [Hz]: %d\n', SampRate);
fprintf(fid, 'Test Time [sec]: %g', TestTime(i));
fprintf(fid, 'Amplitude [mV]:\n');
fprintf(fid, '%g ', Signal{i});
fprintf(fid, '\n');
end
fclose(fid);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!