How to speed up saving data to .txt file?

12 次查看(过去 30 天)
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?

采纳的回答

Jan
Jan 2021-9-2
编辑:Jan 2021-9-26
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by