File Exchange

image thumbnail

write_ascii

version 1.3 (1.58 KB) by

simple and elegant matlab function to quickly write large matrices to an ascii file.

0 Downloads

Updated

View License

Writes large matrices to an ascii file. Format is up to user. Function automatically appends to file. It is many times (10-20X) faster than dlmwrite and almost as fast as using a mex file. Uses sprintf and fwrite (opposed to fprintf).

Example usage:

M = rand(1e5,3)*1000;
tic;dlmwrite('test.txt',M,'delimiter','\t','precision','%9.3f');toc;
tic;write_ascii('test2.txt',M,'\t','%9.3f');toc;
Elapsed time is 7.446049 seconds.
Elapsed time is 0.513611 seconds.

Comments and Ratings (2)

Jan,

It's a few years late, but after changing your code to include the carriage return like mine (personal preference), they appear to run at approximately the same speed for me. Your version is, however, done in a few less lines. Thanks for the suggestion.

-Jacob

Jan Simon

Jan Simon (view profile)

Under Matlab 2011b this is 20% faster for your example data:

nCols = size(data, 2);
fid = fopen(filename, 'w');
Fmt = [repmat([Fmt, delimiter], 1, nCols-1), Fmt, char(10)];
fprintf(fid, Fmt, data');
fclose(fid);

The name "format" shadows a built-in function, therefore I'd prefer "Fmt" here.

Updates

1.3

fixed code comments so help command works

1.2

added example usage in description

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video