-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadYamlFile.m
49 lines (38 loc) · 1.44 KB
/
ReadYamlFile.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% This code is taken from https://github.com/llerussell/ReadYAML %%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function results = ReadYamlFile(filePath)
% Lloyd Russell 2017
% Simple little function to read simple little YAML format parameter files
% read file line by line
fid = fopen(filePath, 'r');
data = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '');
fclose(fid);
% remove empty lines
data = deblank(data{1});
data(cellfun('isempty', data)) = [];
% prepare final results structure
results = [];
% parse the contents (line by line)
for i = 1:numel(data)
% extract this line
thisLine = data{i};
% ignore if this line is a comment
if strcmpi(thisLine(1), '#')
continue
end
% find the seperator between key and value
sepIndex = find(thisLine==':', 1, 'first');
% get the key name (remove whitespace)
key = strtrim(thisLine(1:sepIndex-1));
% get the value, ignoring any comments (remove whitespace)
value = strsplit(thisLine(sepIndex+1:end), '#');
value = strtrim(value{1});
% attempt to convert value to numeric type
[convertedValue, success] = str2num(value);
if success
value = convertedValue;
end
% store the key and value in the results
results.(key) = value;
end