source: cellwrite.m @ 2

Last change on this file since 2 was 2, checked in by tjniskan, 12 years ago
  • Imported first revision
File size: 1.3 KB
Line 
1function [done,msg] = cellwrite(incell,filename,varargin)
2% CELLWRITE - Write cell array to a text file
3%   
4%
5% Synopsis:
6%
7% Description:
8%
9% Examples:
10%
11% See also:
12%
13
14% Juha-Pekka Niskanen - <Juha-Pekka.Niskanen@uku.fi>  -  23.05.2006
15% Department of Biomedical NMR, University of Kuopio
16%
17% Last modified - 23.05.2006 - (J-P N)
18
19% Defaults
20done = false;
21delim = ';';
22
23% Check input arguments
24if nargin<2
25  error('Too few input arguments')
26elseif ~iscell(incell)
27  error('First input argument must be a cell array')
28elseif ~ischar(filename)
29  error('Filename must be of class char')
30end
31
32% Parse Input arguments
33for ii=1:2:length(varargin)
34  switch varargin{ii}
35   case 'delimitter'
36    if strcmpi(varargin{ii+1},'tab')
37      delim = '\t';
38    elseif strcmpi(varargin{ii+1},'space')
39      delim = ' ';
40    else
41      delim=varargin{ii+1};
42    end
43   otherwise
44    msg = sprintf('Unknown parameter "%s"!',varargin{ii});
45    return
46  end
47end
48
49% Open file for writing
50fid = fopen(filename,'w');
51if fid<0
52  msg = sprintf('Could not open file "%s" for writing',filename);
53  return
54end
55
56nRows = size(incell,1);
57nCols = size(incell,2);
58
59% Write cell to file
60for ii=1:nRows
61  for kk=1:nCols
62    fprintf(fid,['%s',delim],incell{ii,kk});
63  end
64  fprintf(fid,'\r\n');
65end
66
67% Close file
68fclose(fid);
69
70% All went well...
71done=true;
72msg='';
73
74% - EOF -
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008