/
ezwrite.m
73 lines (63 loc) · 1.56 KB
/
ezwrite.m
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
64
65
66
67
68
69
70
71
72
73
function ezwrite(fname,out_struct,varargin)
% Write a CSV file that contains both text and numeric columns
% from the contents of a structure (of the format produced by "EZread"),
% without having to manually specify the column type using %f, %s, etc.
%
% Inputs:
% 1. Filename
% 2. structure to write to the file (vector of data per field in
% structure, as produced by EZread).
%
% Outputs
% - none
%
% Paul Taylor
% The MathWorks Australia Pty Ltd
% 20/02/2007
% Set the file delimiter
if nargin == 3
file_delim = varargin{1};
else
file_delim = ',';
end
% Open the file
fid = fopen(fname,'w');
if fid == -1
% could not open the file
error('Error opening file')
end
headers = fieldnames(out_struct);
% Write header line
for i = 1:length(headers)
fprintf(fid,headers{i});
if i < length(headers)
fprintf(fid,file_delim);
end
end
fprintf(fid,'\n');
% Determine the data types for each field
format_cell = cell(length(headers),1);
for i = 1:length(headers)
if isnumeric(out_struct.(headers{i})(1))
format_cell{i} = 'N';
else
format_cell{i} = 'T';
end
end
% Write the data
for i = 1:length(out_struct.(headers{1}))
for j = 1:length(headers)
switch format_cell{j}
case 'N'
fprintf(fid,'%g',out_struct.(headers{j})(i));
case 'T'
fprintf(fid,'%s',out_struct.(headers{j}){i});
end
if j < length(headers)
fprintf(fid,file_delim);
end
end
fprintf(fid,'\n');
end
% Close the file
fclose(fid);