rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
print_datatable.m
1 function print_datatable(fname, title, values, blocksize)
2 % function print_datatable(fname, title, values, blocksize)
3 %
4 % print a matrix in a tabulator separated table stored in file 'fname'. This
5 % can be used by all pgfplots commandos in LaTeX.
6 %
7 % arguments:
8 % 'fname': file name of file where the table is stored
9 % 'title': vector of column titles
10 % 'values': matrix or 2d-cell array whose rows are written to the table
11 % 'blocksize': separate blocksize values with a new line in each column,
12 % needed for patch plots
13 %
14 
15 if nargin < 4
16  blocksize = size(values,2);
17 end
18 
19 numblocks = size(values,2)/blocksize;
20 
21 assert(numblocks-floor(numblocks)==0);
22 
23 fid=fopen(fname,'w');
24 
25 assert(length(title) == size(values, 1));
26 
27 fpstring = [repmat('%s\t', 1, length(title)), '\n'];
28 fprintf(fid, fpstring, title{:});
29 if iscell(values)
30  fpstring2 = cellfun(@formatter, values(:,1) , 'UniformOutput', false);
31  fpstring2 = [fpstring2{:}, '\n'];
32 else
33  fpstring2 = [repmat('%e\t', 1, length(title)), '\n'];
34 end
35 for i=1:numblocks
36  if iscell(values)
37  fprintf(fid, fpstring2, values{:,(i-1)*blocksize+(1:blocksize)});
38  else
39  fprintf(fid, fpstring2, values(:,(i-1)*blocksize+(1:blocksize)));
40  end
41  fprintf(fid, '\n');
42 end
43 
44 fclose(fid);
45 
46 end
47 
48 function ft = formatter(value)
49  if isnumeric(value)
50  ft = '%e\t';
51  elseif ischar(value)
52  ft = '%s\t';
53  elseif islogical(value)
54  ft = '%d\t';
55  else
56  error('Do not know how to print out the values in cell array...');
57  end
58 end