forked from m053m716/-io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_wrist_task_txt_header.m
72 lines (71 loc) · 1.89 KB
/
parse_wrist_task_txt_header.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
function h = parse_wrist_task_txt_header(fname)
%PARSE_WRIST_TASK_TXT_HEADER Parse header of TrialData.txt file.
%
% Syntax:
% h = io.parse_wrist_task_txt_header(fname);
%
% Inputs:
% fname - Full filename of txt file to parse. First row of this file
% should be 'UTF-8' encoded sets of <variable>:<value> with
% whitespace delimiter between each pair.
% e.g.
% Version:5.1 Session:uRfVdkFCAGZKsBqW Subject:Spencer ...
%
% Output:
% h - Header struct with version information and then each of the
% fields (the first part of colon-delimited pairs) and their
% corresponding value.
%
% See also: Contents, io.import_wrist_task_trial_data
fid = fopen(fname);
txt = fgets(fid);
header_vars = strsplit(txt, '\t');
try
txt = fgets(fid);
catch
h.Version = 0.1;
h.var_line = 1;
h.start_line = 2;
h.num_vars = numel(header_vars);
h.var_names = string(header_vars);
return;
end
next_vars = strsplit(txt, '\t');
fclose(fid);
h = struct;
if contains(header_vars{1}, 'Version')
for iV = 1:numel(header_vars)
tmp = strsplit(header_vars{iV}, ':');
data = str2double(tmp{2});
if ~isnan(data)
val = data;
else
val = tmp{2};
end
h.(tmp{1}) = val;
end
h.num_vars = numel(next_vars);
h.var_names = string(next_vars);
h.var_line = 2;
h.start_line = 3;
else
h.Version = struct;
h.Version.Major = 0;
h.Version.Minor = 1;
h.num_vars = numel(header_vars);
h.var_names = string(header_vars);
h.var_line = 1;
h.start_line = 2;
end
if ~isstruct(h.Version)
tmp = num2str(h.Version);
h.Version = struct;
tmp_parts = strsplit(tmp, '.');
h.Version.Major = str2double(tmp_parts{1});
if numel(tmp_parts) == 1
h.Version.Minor = 0;
else
h.Version.Minor = str2double(tmp_parts{2});
end
end
end