Is it possible to increase the speed of writing a delimited text file using "writetable" and if so how?

6 views (last 30 days)
Writing a table data structure containing 50,000 lines and 18 columns (all entries are numbers) takes about 50 seconds but reading the resulting file in with "readtable" takes about 0.8 seconds. I don't understand why writetable is so much slower than readtable.
I need to share the resulting file with people who don't use Matlab, so saving the table data structure as a *.mat binary file will not work.

Answers (1)

per isakson
per isakson on 2 Jun 2015
Edited: per isakson on 3 Jun 2015
Caveat: I've never used the new &nbsp table !
  • "I don't understand why ..." &nbsp I assume that regarding writetable speed was not a priority.
  • "people who don't use Matlab" &nbsp I assume they want a text file.
  • Timing "I/O" is a bit tricky because the result depends on the state of the system cache, which changes over time.
  • fprintf &nbsp is the best way to write to text when it comes to speed.
I made a little experiment on R2013a and a 8 year old vanilla PC desktop. Run it on your computer. It indicates that a ten time increase in writing speed is possible.
>> et = cssm([50000,18]);
>> et
et =
4.6681 0.4383 0.5839
where
function et = cssm( sz )
%{
cssm([3,5]);
et = cssm([50000,18]);
%}
M1 = randi( 9, sz );
tic
frm = repmat( '%g ', [ 1, sz(2) ] );
frm = [ frm(1:end-1), '\n' ];
fid = fopen('c:\tmp\test.txt','w');
for rr = 1 : sz(1)
fprintf( fid, frm, M1( rr, : ) );
end
fclose( fid );
et = toc;
tic
fid = fopen('c:\tmp\test.txt','r');
M2 = textscan( fid, repmat('%f',[1,sz(2)]) ...
, 'Delimiter',' ', 'CollectOutput', true );
fclose( fid );
et = [ et, toc ];
tic
fid = fopen('c:\tmp\test.txt','r');
M3 = fscanf( fid, '%f', sz );
fclose( fid );
et = [ et, toc ];
end
Thus try
array = table2array( your_numerical_table );
...
for rr = 1 : sz(1)
fprintf( fid, frm, array( rr, : ) );
end

Community Treasure Hunt

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

Start Hunting!