- "I don't understand why ..."   I assume that regarding writetable speed was not a priority.
- "people who don't use Matlab"   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   is the best way to write to text when it comes to speed.
Is it possible to increase the speed of writing a delimited text file using "writetable" and if so how?
6 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
per isakson
on 2 Jun 2015
Edited: per isakson
on 3 Jun 2015
Caveat: I've never used the new   table !
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
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!