forked from arokem/python-matlab-bridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmat2json.m
78 lines (74 loc) · 2.11 KB
/
mat2json.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 J=mat2json(M,F)
%MAT2JSON: converts a Matlab structure into a javscript data object (JSON).
%
% J = MAT2JSON(M,F)
%
% M can also be a file name. In the spirit of fast prototyping
% this function takes a very loose approach to data types and
% dimensionality - neither is explicitly retained.
%
% The second input argument is optional and when used it indicates
% the name of the file where J is to be stored.
%
% Example: mat2json(json2mat('{lala:2,lele:4,lili:[1,2,{bubu:5}]}'))
%
% Jonas Almeida, March 2010
switch class(M)
case 'struct'
J='{';
f=fieldnames(M);
for i=1:length(f)
J=[J,'"',f{i},'":',mat2json(M.(f{i})),','];
end
J(end)='}';
case 'cell'
J='[';
for i=1:length(M)
J=[J,mat2json(M{i}),','];
end
if J == '[';
J='[]';
else
J(end) = ']';
end
otherwise
if isnumeric(M) % notice looseness in not converting single numbers into arrays
if length(M(:))==1
J=num2str(M);
else
s=size(M);
if (length(s)==2)&(s(1)<2) % horizontal or null vector
J=['[',num2str(M),']']; % and of destroying dimensionality
J=regexprep(J,'\s+',',');
elseif length(s)==2 %2D solution
J='[';
for i=1:s(1)
J=[J,mat2json(M(i,:)),','];
end
J(end)=']';
elseif length(s)>2 % for now treat higher dimensions as linear vectors
J=['[',num2str(M(:)'),']']; % and of destroying dimensionality
J=regexprep(J,'\s+',',');
end
end
elseif ischar(M)
% see http://json.org/
M = strrep(M,'\','\\'); % reverse solidus; have to do this one first!
M = strrep(M,'/','\/'); % solidus
M = strrep(M,'"','\"'); % quotation marks
M = strrep(M,char(8),'\b'); % backspace
M = strrep(M,char(12),'\f'); % form feed
M = strrep(M,char(10),'\n'); % newline
M = strrep(M,char(13),'\r'); % carriage return
M = strrep(M,char(9),'\t'); % horizontal tab
J=['"',M,'"']; % otherwise it is treated as a string
else % punt!
J=['"',M,'"']; % otherwise it is treated as a string
end
end
if nargin>1 %save JSON result in file
fid=fopen(F,'w');
fprintf(fid,'%s',J);
fclose(fid);
end
end %function