File: WriteStructsToText.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (63 lines) | stat: -rw-r--r-- 1,667 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function WriteStructsToText(filename,theStructs)
% WriteStructsToText(filename,theStructs)
%
% Write a tab delimited text file.  The first row should
% contain the field names for a structure.  Each following
% row contains the data for one instance of that structure.
%
% This routine writes each element of the structure array as a row.
% Only numeric and string field values are supported.
%
% If the filename is a string, this routine handles opening and
% closing of the file.  Otherwise, this routine assumes
% that what was passed is a valid fid and uses it as such,
% leaving the calling routine to do the opening and closing.
%
% 06/16/03 dhb  Wrote it.
% 07/01/03 dhb  Support string fields too.
% 08/12/06 dhb  Call filetype only if OS9.
% 07/07/13 dhb  Add handling of non-string filename as fid.

% Open the file
if (ischar(filename))
  fid = fopen(filename,'wt');
else
  fid = filename;
end
if (fid == -1)
  error('Error opening file or invalid fid passed');
end

% Get the fieldnames
theFields = fieldnames(theStructs(1));
nFields = length(theFields);
for i = 1:nFields
  fprintf(fid,'%s',theFields{i});
  if (i < nFields)
    fprintf(fid,'\t');
  else
    fprintf(fid,'\n');
  end
end

% Now write each struct's data as a line
nStructs = length(theStructs);
for j = 1:nStructs
  for i = 1:nFields	
    if (ischar(getfield(theStructs(j),theFields{i})))
      fprintf(fid,'%s',getfield(theStructs(j),theFields{i}));
    else
      fprintf(fid,'%g',getfield(theStructs(j),theFields{i}));
    end
    if (i < nFields)
      fprintf(fid,'\t');
    else
      fprintf(fid,'\n');
    end
  end
end

% Close the file.
if (ischar(filename))
  fclose(fid);
end