How to speed up saving data to .txt file?

조회 수: 15 (최근 30일)
AStro
AStro 2021년 9월 2일
편집: Jan 2021년 9월 26일
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