Skip to content

Remove eval statements, replace string cells with string arrays, rewrite struct2json and cell2json, add m2json test cases, fix convertDate #506

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 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 61 additions & 6 deletions plotly/plotly_aux/Test_m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,29 @@ function test2dArrayInRange0to10(tc)

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

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

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

function testLogScaledVariables(tc)
values = 1e14 + 10.^(1:5) + 0.23456789;
expected = "[1e+14,1.000000000001e+14,1.00000000001e+14,1.0000000001e+14,1.000000001e+14]";
expected = "[1e+14,1.000000000001e+14,1.00000000001e+14," ...
+ "1.0000000001e+14,1.000000001e+14]";
tc.verifyEqual(string(m2json(values)), expected);
end

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

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

function testInRangeMinus1e14Plus0to1(tc)
values = -1e14 + (1:5) + 0.23456789;
expected = "[-99999999999998.8,-99999999999997.8,-99999999999996.8,-99999999999995.8,-99999999999994.8]";
expected = "[-99999999999998.8,-99999999999997.8," ...
+ "-99999999999996.8,-99999999999995.8," ...
+ "-99999999999994.8]";
tc.verifyEqual(string(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);
end

function testStruct(tc)
values = struct("a", 1, "b", "text");
expected = "{""a"" : 1, ""b"" : ""text""}";
tc.verifyEqual(string(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);
end

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

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

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

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

function testString(tc)
value = "World";
expected = """World""";
tc.verifyEqual(string(m2json(value)), expected);
end
end
end
12 changes: 3 additions & 9 deletions plotly/plotly_aux/cell2json.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
function str = cell2json(s)
str = '';
for i =1:length(s)
val = s{i};
valstr = m2json(val);
str = [str ', ' valstr];
end
str = str(3:end); % snip leading comma
str = ['[' str ']'];
end
strList = string(cellfun(@m2json, s, un=0));
str = sprintf("[%s]", strjoin(strList, ", "));
end
20 changes: 13 additions & 7 deletions plotly/plotly_aux/m2json.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
valstr = cell2json(val);
elseif isa(val, "numeric")
sz = size(val);
numDigits = 3 + ceil(clip(log10(double(max(abs(val),[],"all"))) ...
- log10(double(range(val,"all"))),0,12));
numDigits(~isfinite(numDigits)) = 7;
numDigits = max(arrayfun(@getPrecision, val));
if isa(val,"single")
numDigits = min(7, numDigits);
else
numDigits = min(15, numDigits);
end
fmt = sprintf("%%.%ig", numDigits);
if sum(sz>1)>1 % 2D or higher array
valsubstr = strings(1, sz(1));
Expand All @@ -21,7 +24,11 @@
valstr = arrayfun(@(x) sprintf(fmt, x), val);
valstr = strjoin(valstr, ",");
end
valstr = "[" + valstr + "]";
if length(val)>1
valstr = "[" + valstr + "]";
elseif isempty(val)
valstr = "[]";
end
valstr = strrep(valstr,"-Inf", "null");
valstr = strrep(valstr, "Inf", "null");
valstr = strrep(valstr, "NaN", "null");
Expand Down Expand Up @@ -55,7 +62,6 @@
end
end

function x = clip(x,lb,ub)
x(x<lb) = lb;
x(x>ub) = ub;
function numDigits = getPrecision(val)
numDigits = strlength(sprintf("%.15g", val));
end
10 changes: 2 additions & 8 deletions plotly/plotly_aux/struct2json.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
function str = struct2json(s)
f = fieldnames(s);
str = '';
for i = 1:length(fieldnames(s))
val = s.(f{i});
valstr = m2json(val);
str = [str '"' f{i} '"' ': ' valstr ', ' ];
end
str = str(1:(end-2)); % trim trailing comma
str = ['{' str '}'];
strList = cellfun(@(x) sprintf('"%s" : %s', x, m2json(s.(x))), f, un=0);
str = sprintf("{%s}", strjoin(strList, ", "));
end
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/core/updateHeatmapAnnotation.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
if obj.State.Text(anIndex).Title

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-x position-%
obj.layout.annotations{axIndex}.x = mean(xaxis.domain);
Expand Down
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/core/updateLegendMultipleAxes.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

axIndex = obj.getAxisIndex(obj.State.Plot(traceIndex).AssociatedAxis);
[xSource, ySource] = findSourceAxis(obj, axIndex);
xAxis = eval(sprintf('obj.layout.xaxis%d', xSource));
yAxis = eval(sprintf('obj.layout.yaxis%d', xSource));
xAxis = obj.layout.("xaxis" + xSource);
yAxis = obj.layout.("yaxis" + xSource);

allDomain(traceIndex, 1) = max(xAxis.domain);
allDomain(traceIndex, 2) = max(yAxis.domain);
Expand Down
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/handlegraphics/updateAnimatedLine.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function updateAnimatedLine(obj,plotIndex)
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-------------------------------------------------------------------------%

Expand Down
2 changes: 1 addition & 1 deletion plotly/plotlyfig_aux/handlegraphics/updateBar3.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
axis_data = ancestor(bar_data.Parent,'axes');

%-GET SCENE-%
eval(['scene = obj.layout.scene' num2str(xsource) ';']);
scene = obj.layout.("scene" + xsource);

%-------------------------------------------------------------------------%

Expand Down
2 changes: 1 addition & 1 deletion plotly/plotlyfig_aux/handlegraphics/updateBar3h.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
axis_data = ancestor(bar_data.Parent,'axes');

%-GET SCENE-%
eval(['scene = obj.layout.scene' num2str(xsource) ';']);
scene = obj.layout.("scene" + xsource);

%-------------------------------------------------------------------------%

Expand Down
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/handlegraphics/updateBarseries.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-------------------------------------------------------------------------%

Expand Down
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/handlegraphics/updateBoxplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%---------------------------------------------------------------------%

Expand Down
10 changes: 5 additions & 5 deletions plotly/plotlyfig_aux/handlegraphics/updateCategoricalHistogram.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-------------------------------------------------------------------------%

Expand Down Expand Up @@ -90,9 +90,9 @@
xmax = (hist_data.NumDisplayBins - 1) + gap;

t = 'category';
eval(['obj.layout.xaxis' num2str(xsource) '.type = t;']);
eval(['obj.layout.xaxis' num2str(xsource) '.autotick = false;']);
eval(['obj.layout.xaxis' num2str(xsource) '.range = {xmin, xmax};']);
obj.layout.("xaxis" + xsource).type = t;
obj.layout.("xaxis" + xsource).autotick = false;
obj.layout.("xaxis" + xsource).range = {xmin, xmax};

%-------------------------------------------------------------------------%

Expand Down
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/handlegraphics/updateComet.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ function updateComet(obj,plotIndex)
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-------------------------------------------------------------------------%

Expand Down
2 changes: 1 addition & 1 deletion plotly/plotlyfig_aux/handlegraphics/updateConeplot.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-SCENE DATA-%
eval(['scene = obj.layout.scene' num2str(xsource) ';']);
scene = obj.layout.("scene" + xsource);

%-------------------------------------------------------------------------%

Expand Down
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/handlegraphics/updateContour3.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-------------------------------------------------------------------------%

Expand Down
4 changes: 2 additions & 2 deletions plotly/plotlyfig_aux/handlegraphics/updateContourProjection.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-------------------------------------------------------------------------%

Expand Down
2 changes: 1 addition & 1 deletion plotly/plotlyfig_aux/handlegraphics/updateFmesh.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
axisData = ancestor(meshData.Parent,'axes');

%-SCENE DATA-%
eval( sprintf('scene = obj.layout.scene%d;', xsource) );
scene = obj.layout.("scene" + xsource);

%-GET CONTOUR INDEX-%
obj.PlotOptions.nPlots = obj.PlotOptions.nPlots + 1;
Expand Down
22 changes: 11 additions & 11 deletions plotly/plotlyfig_aux/handlegraphics/updateFunctionContour.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
[xsource, ysource] = findSourceAxis(obj,axIndex);

%-AXIS DATA-%
eval(['xaxis = obj.layout.xaxis' num2str(xsource) ';']);
eval(['yaxis = obj.layout.yaxis' num2str(ysource) ';']);
xaxis = obj.layout.("xaxis" + xsource);
yaxis = obj.layout.("yaxis" + ysource);

%-------------------------------------------------------------------------%

Expand Down Expand Up @@ -214,15 +214,15 @@

%-axis layout-%
t = 'linear';
eval(['obj.layout.xaxis' num2str(xsource) '.type=t;']);
eval(['obj.layout.xaxis' num2str(xsource) '.autorange=true;']);
eval(['obj.layout.xaxis' num2str(xsource) '.ticktext=axis_data.XTickLabel;']);
eval(['obj.layout.xaxis' num2str(xsource) '.tickvals=axis_data.XTick;']);

eval(['obj.layout.yaxis' num2str(xsource) '.type=t;']);
eval(['obj.layout.yaxis' num2str(xsource) '.autorange=true;']);
eval(['obj.layout.yaxis' num2str(xsource) '.ticktext=axis_data.YTickLabel;']);
eval(['obj.layout.yaxis' num2str(xsource) '.tickvals=axis_data.YTick;']);
obj.layout.("xaxis" + xsource).type=t;
obj.layout.("xaxis" + xsource).autorange=true;
obj.layout.("xaxis" + xsource).ticktext=axis_data.XTickLabel;
obj.layout.("xaxis" + xsource).tickvals=axis_data.XTick;

obj.layout.("yaxis" + xsource).type=t;
obj.layout.("yaxis" + xsource).autorange=true;
obj.layout.("yaxis" + xsource).ticktext=axis_data.YTickLabel;
obj.layout.("yaxis" + xsource).tickvals=axis_data.YTick;

%-------------------------------------------------------------------------%

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
axisData = ancestor(meshData.Parent,'axes');

%-SCENE DATA-%
eval( sprintf('scene = obj.layout.scene%d;', xsource) );
scene = obj.layout.("scene" + xsource);

%-GET CONTOUR INDEX-%
obj.PlotOptions.nPlots = obj.PlotOptions.nPlots + 1;
Expand Down
Loading