IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
cell2csv.m
Go to the documentation of this file.
1 %>@ingroup conversion string
2 %> @file
3 %> @brief Creates CSV string to be saved into CSV file
4 %>
5 %
6 %> @param results_table Cell of strings or numbers
7 %> @param deli =TAB. Delimiter
8 %> @return CSV-formatted string
9 function s = cell2csv(results_table, deli)
10 if nargin < 2 || isempty(deli)
11  deli = sprintf('\t');
12 end;
13 
14 chars_elim = '[]''';
15 lce = length(chars_elim);
16 
17 % Converts all elements to str
18 [rows, cols] = size(results_table);
19 ii = 0;
20 for i = 1:rows
21  for j = 1:cols
22  t = results_table{i, j};
23 
24  % Eliminates undesired characters
25  if ~isnumeric(t)
26  stemp = t;
27  else
28  stemp = mat2str(t);
29  end;
30  for k = 1:lce
31  stemp(logical(stemp == chars_elim(k))) = [];
32  end;
33 
34  results_table{i, j} = stemp;
35  end;
36 
37  ii = ii+1;
38  if ii == 5
39  ii = 0;
40  end;
41 end;
42 
43 
44 % Calculates total stream size
45 len_total = 0;
46 for i = 1:rows
47  for j = 1:cols
48  len_total = len_total+length(results_table{i, j});
49  end;
50 end;
51 
52 len_total = len_total+(cols-1)*rows+rows; % first add for \t and second one for \n
53 
54 % Allocates stream
55 s = char(' '*ones(1, len_total));
56 
57 % Writes stream
58 iptr = 1;
59 ii = 0;
60 for i = 1:rows
61  for j = 1:cols
62  ilen = length(results_table{i, j});
63 
64  if j > 1
65  s(iptr) = deli;
66  iptr = iptr+1;
67  end;
68  s(iptr:iptr+ilen-1) = results_table{i, j};
69  iptr = iptr+ilen;
70  end;
71 
72  if j > 1
73  s(iptr) = sprintf('\n');
74  iptr = iptr+1;
75  end;
76 
77  ii = ii+1;
78  if ii == 5
79 % fprintf('%d rowsnow\n', i);
80  ii = 0;
81  end;
82 end;
83 
84 
85 
function cell2csv(in results_table, in deli)