-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreadJSON.m
More file actions
63 lines (45 loc) · 1.28 KB
/
readJSON.m
File metadata and controls
63 lines (45 loc) · 1.28 KB
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
% Read JSON files that contain points and triangles describing the shapes
function shape = readJSON(path)
fileID = fopen(path,'r');
% 1e ligne : {"points":[
a = fgets(fileID);
a = fgets(fileID);
points = [];
% Go through the points
while (isempty(strfind(a,'triangles')))
% Get x
x = str2double(a(10:end-2));
a = fgets(fileID);
% Get y
if a(end-2)==']'
y = str2double(a(10:end-4));
else
y = str2double(a(10:end-3));
end
points = [points ; x y];
a = fgets(fileID);
end
a = fgets(fileID);
triangles = [];
% Go through the triangles
while (a(1)~='}')
% Get p1
p1 = str2double(a(11:end-2));
a = fgets(fileID);
% Get p2
p2 = str2double(a(11:end-2));
a = fgets(fileID);
% Get p3
if a(end-2)==']'
p3 = str2double(a(11:end-4));
else
p3 = str2double(a(11:end-3));
end
triangles = [triangles ; p1 p2 p3];
a = fgets(fileID);
end
triangles = triangles + 1;
fclose(fileID);
shape.points = points;
shape.triangles = triangles;
end