Skip to content

Commit ba57520

Browse files
Add m2json test cases, fix convertDate
1 parent 5b1b0f1 commit ba57520

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

plotly/plotly_aux/Test_m2json.m

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,29 @@ function test2dArrayInRange0to10(tc)
1515

1616
function testInRange1e6to1e5(tc)
1717
values = 1e-6 * (1 + (1:5) + 0.23456789);
18-
expected = "[2.235e-06,3.235e-06,4.235e-06,5.235e-06,6.235e-06]";
18+
expected = "[2.235e-06,3.235e-06,4.235e-06,5.235e-06," ...
19+
+ "6.235e-06]";
1920
tc.verifyEqual(string(m2json(values)), expected);
2021
end
2122

2223
function testInRange1e14Plus0to1(tc)
2324
values = 1e14 + (1:5) + 0.23456789;
24-
expected = "[100000000000001,100000000000002,100000000000003,100000000000004,100000000000005]";
25+
expected = "[100000000000001,100000000000002,"...
26+
+ "100000000000003,100000000000004,100000000000005]";
2527
tc.verifyEqual(string(m2json(values)), expected);
2628
end
2729

2830
function testInRange1e14Plus1e7Plus0to1(tc)
2931
values = 1e14 + 1e7 + (1:5) + 0.23456789;
30-
expected = "[100000010000001,100000010000002,100000010000003,100000010000004,100000010000005]";
32+
expected = "[100000010000001,100000010000002," ...
33+
+ "100000010000003,100000010000004,100000010000005]";
3134
tc.verifyEqual(string(m2json(values)), expected);
3235
end
3336

3437
function testLogScaledVariables(tc)
3538
values = 1e14 + 10.^(1:5) + 0.23456789;
36-
expected = "[1e+14,1.000000000001e+14,1.00000000001e+14,1.0000000001e+14,1.000000001e+14]";
39+
expected = "[1e+14,1.000000000001e+14,1.00000000001e+14," ...
40+
+ "1.0000000001e+14,1.000000001e+14]";
3741
tc.verifyEqual(string(m2json(values)), expected);
3842
end
3943

@@ -45,14 +49,65 @@ function testInRangeMinus10to0(tc)
4549

4650
function testInRangeMinus1e5toMinus1e6(tc)
4751
values = -1e-6 * (1 + (1:5) + 0.23456789);
48-
expected = "[-2.235e-06,-3.235e-06,-4.235e-06,-5.235e-06,-6.235e-06]";
52+
expected = "[-2.235e-06,-3.235e-06,-4.235e-06,-5.235e-06," ...
53+
+ "-6.235e-06]";
4954
tc.verifyEqual(string(m2json(values)), expected);
5055
end
5156

5257
function testInRangeMinus1e14Plus0to1(tc)
5358
values = -1e14 + (1:5) + 0.23456789;
54-
expected = "[-99999999999998.8,-99999999999997.8,-99999999999996.8,-99999999999995.8,-99999999999994.8]";
59+
expected = "[-99999999999998.8,-99999999999997.8," ...
60+
+ "-99999999999996.8,-99999999999995.8," ...
61+
+ "-99999999999994.8]";
5562
tc.verifyEqual(string(m2json(values)), expected);
5663
end
64+
65+
function testCell(tc)
66+
values = {1, "text", [1,2,3]};
67+
expected = "[1, ""text"", [1,2,3]]";
68+
tc.verifyEqual(string(m2json(values)), expected);
69+
end
70+
71+
function testStruct(tc)
72+
values = struct("a", 1, "b", "text");
73+
expected = "{""a"" : 1, ""b"" : ""text""}";
74+
tc.verifyEqual(string(m2json(values)), expected);
75+
end
76+
77+
function testDatetime(tc)
78+
value = datetime("2023-05-01 12:30:45");
79+
expected = """2023-05-01 12:30:45""";
80+
tc.verifyEqual(string(m2json(value)), expected);
81+
end
82+
83+
function testDate(tc)
84+
value = datetime("2023-05-01");
85+
expected = """2023-05-01""";
86+
tc.verifyEqual(string(m2json(value)), expected);
87+
end
88+
89+
function testLogicalTrue(tc)
90+
value = true;
91+
expected = "true";
92+
tc.verifyEqual(string(m2json(value)), expected);
93+
end
94+
95+
function testLogicalFalse(tc)
96+
value = false;
97+
expected = "false";
98+
tc.verifyEqual(string(m2json(value)), expected);
99+
end
100+
101+
function testCharArray(tc)
102+
value = 'Hello';
103+
expected = """Hello""";
104+
tc.verifyEqual(string(m2json(value)), expected);
105+
end
106+
107+
function testString(tc)
108+
value = "World";
109+
expected = """World""";
110+
tc.verifyEqual(string(m2json(value)), expected);
111+
end
57112
end
58113
end

plotly/plotly_aux/m2json.m

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
valstr = cell2json(val);
66
elseif isa(val, "numeric")
77
sz = size(val);
8-
numDigits = 3 + ceil(clip(log10(double(max(abs(val),[],"all"))) ...
9-
- log10(double(range(val,"all"))),0,12));
10-
numDigits(~isfinite(numDigits)) = 7;
8+
numDigits = max(arrayfun(@getPrecision, val));
9+
if isa(val,"single")
10+
numDigits = min(7, numDigits);
11+
else
12+
numDigits = min(15, numDigits);
13+
end
1114
fmt = sprintf("%%.%ig", numDigits);
1215
if sum(sz>1)>1 % 2D or higher array
1316
valsubstr = strings(1, sz(1));
@@ -21,7 +24,11 @@
2124
valstr = arrayfun(@(x) sprintf(fmt, x), val);
2225
valstr = strjoin(valstr, ",");
2326
end
24-
valstr = "[" + valstr + "]";
27+
if length(val)>1
28+
valstr = "[" + valstr + "]";
29+
elseif isempty(val)
30+
valstr = "[]";
31+
end
2532
valstr = strrep(valstr,"-Inf", "null");
2633
valstr = strrep(valstr, "Inf", "null");
2734
valstr = strrep(valstr, "NaN", "null");
@@ -55,7 +62,6 @@
5562
end
5663
end
5764

58-
function x = clip(x,lb,ub)
59-
x(x<lb) = lb;
60-
x(x>ub) = ub;
65+
function numDigits = getPrecision(val)
66+
numDigits = strlength(sprintf("%.15g", val));
6167
end

plotly/plotlyfig_aux/helpers/convertDate.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
function output = convertDate(date)
22
date = convertToDateTime(date);
33
if isDate(date)
4-
format = "yyyy-mm-dd";
4+
format = "yyyy-MM-dd";
55
else
6-
format = "yyyy-mm-dd HH:MM:ss";
6+
format = "yyyy-MM-dd HH:mm:ss";
77
end
88
output = string(date, format);
99
end
1010

1111
function dt = convertToDateTime(input)
1212
if isdatetime(input)
13+
dt = input;
1314
return
1415
elseif isnumeric(input)
1516
% Assume input is a datenum

0 commit comments

Comments
 (0)