Skip to content

Fix empty m2json behaviour, fix colour issues #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix m2json case where val is empty
  • Loading branch information
robertoffmoura committed Aug 12, 2024
commit 52f5d8097715ae85ff6b514321f638e8207ed169
46 changes: 26 additions & 20 deletions plotly/plotly_aux/Test_m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,132 +3,138 @@
function testLowPrecisionInRange0to10(tc)
values = 1 + (1:5) + 0.234;
expected = "[2.234,3.234,4.234,5.234,6.234]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRange0to10(tc)
values = 1 + (1:5) + 0.23456789;
expected = "[2.23456789,3.23456789,4.23456789,5.23456789," ...
+ "6.23456789]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function test2dArrayInRange0to10(tc)
values = 1 + (1:5) + (0:1)' + 0.234;
expected = "[[2.234,3.234,4.234,5.234,6.234]," ...
+ "[3.234,4.234,5.234,6.234,7.234]]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testLowPrecisionInRange1e6to1e5(tc)
values = 1e-6 * (1 + (1:5) + 0.234);
expected = "[2.234e-06,3.234e-06,4.234e-06,5.234e-06," ...
+ "6.234e-06]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRange1e6to1e5(tc)
values = 1e-6 * (1 + (1:5) + 0.23456789);
expected = "[2.23456789e-06,3.23456789e-06,4.23456789e-06," ...
+ "5.23456789e-06,6.23456789e-06]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRange1e14Plus0to1(tc)
values = 1e14 + (1:5) + 0.23456789;
expected = "[100000000000001,100000000000002,"...
+ "100000000000003,100000000000004,100000000000005]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRange1e14Plus1e7Plus0to1(tc)
values = 1e14 + 1e7 + (1:5) + 0.23456789;
expected = "[100000010000001,100000010000002," ...
+ "100000010000003,100000010000004,100000010000005]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testLogScaledVariables(tc)
values = 1e14 + 10.^(1:5) + 0.23456789;
expected = "[100000000000010,100000000000100," ...
+ "100000000001000,100000000010000,100000000100000]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testLowPrecisionInRangeMinus10to0(tc)
values = -(1 + (1:5) + 0.234);
expected = "[-2.234,-3.234,-4.234,-5.234,-6.234]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRangeMinus10to0(tc)
values = -(1 + (1:5) + 0.23456789);
expected = "[-2.23456789,-3.23456789,-4.23456789," ...
+ "-5.23456789,-6.23456789]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRangeMinus1e5toMinus1e6(tc)
values = -1e-6 * (1 + (1:5) + 0.23456789);
expected = "[-2.23456789e-06,-3.23456789e-06," ...
+ "-4.23456789e-06,-5.23456789e-06,-6.23456789e-06]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testInRangeMinus1e14Plus0to1(tc)
values = -1e14 + (1:5) + 0.23456789;
expected = "[-99999999999998.8,-99999999999997.8," ...
+ "-99999999999996.8,-99999999999995.8," ...
+ "-99999999999994.8]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testEmpty(tc)
values = [];
expected = "[]";
tc.verifyEqual(m2json(values), expected);
end

function testCell(tc)
values = {1, "text", [1,2,3]};
expected = "[1, ""text"", [1,2,3]]";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testStruct(tc)
values = struct("a", 1, "b", "text");
expected = "{""a"" : 1, ""b"" : ""text""}";
tc.verifyEqual(string(m2json(values)), expected);
tc.verifyEqual(m2json(values), expected);
end

function testDatetime(tc)
value = datetime("2023-05-01 12:30:45");
expected = """2023-05-01 12:30:45""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testDate(tc)
value = datetime("2023-05-01");
expected = """2023-05-01""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testLogicalTrue(tc)
value = true;
expected = "true";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testLogicalFalse(tc)
value = false;
expected = "false";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testCharArray(tc)
value = 'Hello';
expected = """Hello""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end

function testString(tc)
value = "World";
expected = """World""";
tc.verifyEqual(string(m2json(value)), expected);
tc.verifyEqual(m2json(value), expected);
end
end
end
8 changes: 5 additions & 3 deletions plotly/plotly_aux/m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
elseif iscell(val)
valstr = cell2json(val);
elseif isa(val, "numeric")
if isempty(val)
valstr = "[]";
return;
end
sz = size(val);
if isa(val,"single")
numDigits = 7;
Expand All @@ -25,8 +29,6 @@
end
if length(val)>1
valstr = "[" + valstr + "]";
elseif isempty(val)
valstr = "[]";
end
valstr = strrep(valstr,"-Inf", "null");
valstr = strrep(valstr, "Inf", "null");
Expand All @@ -38,7 +40,7 @@
valstr = cell2json(cellstr(val));
else
val = checkescape(val); % add escape characters
valstr = sprintf('"%s"', val);
valstr = sprintf("""%s""", val);
end
elseif islogical(val)
if val
Expand Down