-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetInfoLabview.m
66 lines (60 loc) · 1.89 KB
/
getInfoLabview.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
function info = getInfoLabview(path)
%%
f = fopen(path,'r');
s = (fread(f,'*char'))';
N = length(s);
s = double(s);
s(s == 32 | s == 9 | s== 10 | s == 13) =[]; % remove space, tab and newline char
s = char(s);
info = [];
mapv = strfind(s,',');
mapb = strfind(s,':');
mapc = strfind(s,'}');
i = 2; % first char is always '{'
mapv(end+1) = mapc(end);
mapv(mapv < i) = []; mapb(mapb < i) = []; mapc(mapc < i) = [];
while i < N
field = getNextField(s(i:mapb(1)));
if strcmp(s(i+numel(field)+3),'{') == 0 % if the next : is not followed by a {
%grab the field
[field,value] = getNextField(s(i:mapv(1)));
info.(field) = value;
% set the cursor after the
i = mapv(1)+1;
else % there is a subfield
% routine to extract the subfields names and values
sub = s(i:mapc(1)); % subfield name
g = strfind(sub,'"');
v = strfind(sub,',');
subname = sub(g(1)+1:g(2)-1); subname = strrep(subname,' ','_');
g(1:2) = []; v(end+1) = length(sub);
Nsubfields = length(v); % compute # subfields using # of ,
ii = g(1);
for k = 1:Nsubfields
[field,value] = getNextField(sub(ii:v(k)));
info.(subname).(field) = value;
ii = v(k)+1;
end
i = i+length(sub)+1;
end
mapv(mapv < i) = []; mapb(mapb < i) = []; mapc(mapc < i) = [];
if isempty(mapb)
i = N;
end
end
fclose(f);
end
function [field,value] = getNextField(s)
g = strfind(s,'"');
field = s(g(1)+1:g(2)-1);
value = s(g(2)+2:end-1);
field = strrep(field,' ','_');
field = strrep(field,'?','_');
field = strrep(field,'/','_');
if ~isempty(str2num(value))
value = str2num(value);
end
if ~isnan(str2double(field(1))) % if the first char is not a letter
field = ['z_',field];
end
end