forked from shanqing-cai/commonmcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_struct_from_text.m
47 lines (39 loc) · 1.18 KB
/
read_struct_from_text.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
% read_struct_from_test: read a structure from an input text file that contains the field anmes and values
%
% Author: Shanqing Cai (shanqing.cai@gmail.com)
% Date: 2013-02-08
function st = read_struct_from_text(infn)
intxt = textread(infn, '%s', 'delimiter', '\n');
st = struct;
for i1 = 1 : numel(intxt)
t_line = deblank(intxt{i1});
if isempty(t_line)
continue;
end
if isequal(t_line(1), '%')
continue;
end
idx_ds = strfind(t_line, '%');
if ~isempty(idx_ds)
t_line = t_line(1 : idx_ds(1) - 1);
end
if isempty(t_line)
continue;
end
t_items = splitstring(t_line);
if ~(length(t_items) == 1 || length(t_items) == 2)
fprintf(2, 'WARNING: %s: not exactly one or two items found in line %d of text file %s\n', ...
mfilename, i1, infn);
continue;
end
if length(t_items) == 1
st.(t_items{1}) = '';
else
if ~isnan(str2double(t_items{2}))
st.(t_items{1}) = str2double(t_items{2});
else
st.(t_items{1}) = t_items{2};
end
end
end
return