forked from erhanbas/navigator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
configparser.m
80 lines (71 loc) · 1.62 KB
/
configparser.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
74
75
76
77
78
function [out] = configparser(configfile)
%CONFIGPARSER reads a config file and return a structure array based on the
%fields of the configfile
%
% [OUTPUTARGS] = CONFIGPARSER(configfile)
%
% Inputs:
%
% Outputs:
%
% Examples:
%
% See also:
% $Author: base $ $Date: 2016/01/14 15:55:07 $ $Revision: 0.1 $
% Copyright: HHMI 2016
if nargin<1
configfile = 'myparam'
end
fid = fopen(configfile);
out = [];
tline = fgetl(fid);
while ischar(tline)
if isempty(tline)
else
out = checkcase(tline,out);
end
tline = fgetl(fid);
end
fclose(fid);
gt=fieldnames(out);
for ii=1:length(gt)
fieldtxt = strtrim(gt{ii});
txt = strtrim(out.(gt{ii}));
if strcmp(gt{ii},'HEADER')
elseif strcmp(gt{ii},'Tform')
out.(fieldtxt) = cellfun(@str2num,txt);
else
if iscell(txt)
out.(fieldtxt) = cellfun(@str2num,txt);
elseif txt(1)=='''' % string, most likely path
out.(fieldtxt) = eval(txt);
else
out.(fieldtxt) = str2num(txt);
end
end
end
end
function out = checkcase(tline,out)
if tline(1)=='#' % header skip
if isfield(out,'HEADER')
out.HEADER = sprintf('%s%s\n',out.HEADER,tline);
else
out.HEADER = sprintf('%s\n',tline);
end
else
fd = strsplit(tline,'=');
if length(fd)<2
% check for : as splitter
fd = strsplit(tline,':');
end
if length(fd)<2
error('Couldnt find any argument with "=" or ":"')
end
fd_sub = strsplit(strtrim(fd{2}),' ');
if length(fd_sub)>2
out.(deblank(fd{1})) = fd_sub;
else
out.(deblank(fd{1})) = fd{2};
end
end
end