Home > utility > fileWrite.m

fileWrite

PURPOSE ^

fileWrite: Write a cell string to a file

SYNOPSIS ^

function fileWrite(cellStr, fileName)

DESCRIPTION ^

fileWrite: Write a cell string to a file
    Usage: fileWrite(cellStr, fileName)
        cellStr: Cell string to be written
        fileName: Output file

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function fileWrite(cellStr, fileName)
0002 %fileWrite: Write a cell string to a file
0003 %    Usage: fileWrite(cellStr, fileName)
0004 %        cellStr: Cell string to be written
0005 %        fileName: Output file
0006 
0007 %    Roger Jang, 20020618
0008 
0009 if nargin==0, selfdemo; return; end
0010 if isstr(cellStr)    % cellStr is a string
0011     cellStr={cellStr};
0012 end
0013 
0014 fid = fopen(fileName, 'w');
0015 if fid<0,
0016     errorMessage=['Cannot open "', fileName, '"!'];
0017     error(errorMessage);
0018 end
0019 
0020 for i=1:length(cellStr),
0021     fprintf(fid, '%s\r\n', cellStr{i});
0022 end
0023 
0024 fclose(fid);
0025 
0026 % ====== self demo
0027 function selfdemo
0028 fileName = [tempdir, 'test.txt'];
0029 fprintf('fileName=%s\n', fileName);
0030 cellStr={'This', 'is', 'a', 'test.'};
0031 fprintf('cellStr = ');
0032 disp(cellStr);
0033 feval(mfilename, cellStr, fileName);
0034 fprintf('The contents of "cellStr" have been written to "%s":\n', fileName);
0035 type(fileName);

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