Home > utility > asciiWrite.m

asciiWrite

PURPOSE ^

asciiWrite: Write a matrix as an ascii file

SYNOPSIS ^

function asciiWrite(mat, fileName, format)

DESCRIPTION ^

 asciiWrite: Write a matrix as an ascii file
    Usage: asciiWrite(mat, fileName, format)
        format: format string, such as '%d', '%f', etc.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function asciiWrite(mat, fileName, format)
0002 % asciiWrite: Write a matrix as an ascii file
0003 %    Usage: asciiWrite(mat, fileName, format)
0004 %        format: format string, such as '%d', '%f', etc.
0005 
0006 %    Roger Jang, 20041021
0007 
0008 if nargin<1, selfdemo; return; end
0009 if nargin<2, fileName='asciiFile.txt'; end
0010 if nargin<3, format='%g'; end
0011 
0012 fid = fopen(fileName, 'w');
0013 for i = 1:size(mat,1),
0014     fprintf(fid, [format, ' '], mat(i, :));
0015     fprintf(fid, '\n');
0016 end
0017 fclose(fid);
0018 
0019 % ====== Self demo
0020 function selfdemo
0021 x=magic(5);
0022 fileName=tempname;
0023 asciiWrite(x, fileName, '%d');
0024 fprintf('The content of %s is\n', fileName);
0025 type(fileName);

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003