How to speed up saving data to .txt file?

11 visualizzazioni (ultimi 30 giorni)
AStro
AStro il 2 Set 2021
Modificato: Jan il 26 Set 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?

Risposta accettata

Jan
Jan il 2 Set 2021
Modificato: Jan il 26 Set 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);

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by