From a0c468724872a4081242595e9a4153ffd0424187 Mon Sep 17 00:00:00 2001 From: robertoffmoura Date: Mon, 5 Aug 2024 10:47:02 +0100 Subject: [PATCH 1/4] Replace inefficient eval statements --- .../core/updateHeatmapAnnotation.m | 4 ++-- .../core/updateLegendMultipleAxes.m | 4 ++-- .../handlegraphics/updateAnimatedLine.m | 4 ++-- .../plotlyfig_aux/handlegraphics/updateBar3.m | 2 +- .../handlegraphics/updateBar3h.m | 2 +- .../handlegraphics/updateBarseries.m | 4 ++-- .../handlegraphics/updateBoxplot.m | 4 ++-- .../updateCategoricalHistogram.m | 10 ++++----- .../handlegraphics/updateComet.m | 4 ++-- .../handlegraphics/updateConeplot.m | 2 +- .../handlegraphics/updateContour3.m | 4 ++-- .../handlegraphics/updateContourProjection.m | 4 ++-- .../handlegraphics/updateFmesh.m | 2 +- .../handlegraphics/updateFunctionContour.m | 22 +++++++++---------- .../handlegraphics/updateFunctionSurface.m | 2 +- .../handlegraphics/updateHistogram2.m | 10 ++++----- .../handlegraphics/updateImage.m | 4 ++-- .../handlegraphics/updateImage3D.m | 4 ++-- .../updateImplicitFunctionSurface.m | 4 ++-- .../handlegraphics/updateIsosurface.m | 2 +- .../handlegraphics/updateLineseries.m | 2 +- .../plotlyfig_aux/handlegraphics/updateMesh.m | 2 +- .../handlegraphics/updatePColor.m | 4 ++-- .../handlegraphics/updatePatch.m | 4 ++-- .../plotlyfig_aux/handlegraphics/updatePie3.m | 4 ++-- .../handlegraphics/updateQuiver.m | 2 +- .../handlegraphics/updateRectangle.m | 4 ++-- .../handlegraphics/updateScattergroup.m | 4 ++-- .../handlegraphics/updateScatterhistogram.m | 14 ++++++------ .../handlegraphics/updateSlice.m | 2 +- .../handlegraphics/updateStackedplot.m | 8 +++---- .../handlegraphics/updateStreamtube.m | 4 ++-- .../plotlyfig_aux/handlegraphics/updateSurf.m | 2 +- .../handlegraphics/updateSurfaceplot.m | 4 ++-- .../handlegraphics/updateSurfc.m | 2 +- .../handlegraphics/updateSurfl.m | 2 +- .../handlegraphics/updateTernaryContour.m | 21 ++++++++---------- .../handlegraphics/updateTernaryPlot.m | 21 ++++++++---------- .../handlegraphics/updateTernaryPlotPro.m | 21 ++++++++---------- .../helpers/extractHeatmapAxisData.m | 8 +++---- 40 files changed, 112 insertions(+), 121 deletions(-) diff --git a/plotly/plotlyfig_aux/core/updateHeatmapAnnotation.m b/plotly/plotlyfig_aux/core/updateHeatmapAnnotation.m index ba43acc8..808105f9 100644 --- a/plotly/plotlyfig_aux/core/updateHeatmapAnnotation.m +++ b/plotly/plotlyfig_aux/core/updateHeatmapAnnotation.m @@ -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); diff --git a/plotly/plotlyfig_aux/core/updateLegendMultipleAxes.m b/plotly/plotlyfig_aux/core/updateLegendMultipleAxes.m index 06d6b371..6cbe7fbc 100644 --- a/plotly/plotlyfig_aux/core/updateLegendMultipleAxes.m +++ b/plotly/plotlyfig_aux/core/updateLegendMultipleAxes.m @@ -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); diff --git a/plotly/plotlyfig_aux/handlegraphics/updateAnimatedLine.m b/plotly/plotlyfig_aux/handlegraphics/updateAnimatedLine.m index 78b7d133..a504440d 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateAnimatedLine.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateAnimatedLine.m @@ -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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateBar3.m b/plotly/plotlyfig_aux/handlegraphics/updateBar3.m index fc60a552..bf5c5d6a 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateBar3.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateBar3.m @@ -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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateBar3h.m b/plotly/plotlyfig_aux/handlegraphics/updateBar3h.m index ffdc1227..d8185995 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateBar3h.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateBar3h.m @@ -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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateBarseries.m b/plotly/plotlyfig_aux/handlegraphics/updateBarseries.m index a1e60008..70261ec2 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateBarseries.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateBarseries.m @@ -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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateBoxplot.m b/plotly/plotlyfig_aux/handlegraphics/updateBoxplot.m index 36ab77fa..49a01f4c 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateBoxplot.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateBoxplot.m @@ -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); %---------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateCategoricalHistogram.m b/plotly/plotlyfig_aux/handlegraphics/updateCategoricalHistogram.m index dd30d731..e84cd3c9 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateCategoricalHistogram.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateCategoricalHistogram.m @@ -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); %-------------------------------------------------------------------------% @@ -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}; %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateComet.m b/plotly/plotlyfig_aux/handlegraphics/updateComet.m index 9e5105f3..2c62bc4d 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateComet.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateComet.m @@ -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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateConeplot.m b/plotly/plotlyfig_aux/handlegraphics/updateConeplot.m index 15bad50a..84e48661 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateConeplot.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateConeplot.m @@ -10,7 +10,7 @@ [xsource, ysource] = findSourceAxis(obj,axIndex); %-SCENE DATA-% -eval(['scene = obj.layout.scene' num2str(xsource) ';']); +scene = obj.layout.("scene" + xsource); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateContour3.m b/plotly/plotlyfig_aux/handlegraphics/updateContour3.m index 590aedf5..69420ae7 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateContour3.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateContour3.m @@ -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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateContourProjection.m b/plotly/plotlyfig_aux/handlegraphics/updateContourProjection.m index fc124117..f3842cce 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateContourProjection.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateContourProjection.m @@ -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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateFmesh.m b/plotly/plotlyfig_aux/handlegraphics/updateFmesh.m index 5ba48bdc..418f3736 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateFmesh.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateFmesh.m @@ -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; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateFunctionContour.m b/plotly/plotlyfig_aux/handlegraphics/updateFunctionContour.m index 1557891b..36741031 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateFunctionContour.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateFunctionContour.m @@ -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); %-------------------------------------------------------------------------% @@ -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; %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateFunctionSurface.m b/plotly/plotlyfig_aux/handlegraphics/updateFunctionSurface.m index 2f0b573f..1300b8df 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateFunctionSurface.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateFunctionSurface.m @@ -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; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateHistogram2.m b/plotly/plotlyfig_aux/handlegraphics/updateHistogram2.m index 19d1c3b3..de2f8fc5 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateHistogram2.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateHistogram2.m @@ -101,7 +101,7 @@ function updateScene(obj, dataIndex) plotData = obj.State.Plot(dataIndex).Handle; axisData = plotData.Parent; [xSource, ~] = findSourceAxis(obj, axIndex); - scene = eval( sprintf('obj.layout.scene%d', xSource) ); + scene = obj.layout.("scene" + xSource); aspectRatio = axisData.PlotBoxAspectRatio; cameraPosition = axisData.CameraPosition; @@ -163,15 +163,15 @@ function updateScene(obj, dataIndex) function ax = getSceneAxis(axisData, axName) %-initializations-% - axx = eval(sprintf('axisData.%sAxis', axName)); + axx = axisData.(axName + "Axis"); ax.zeroline = false; ax.showline = true; ax.showspikes = true; ax.linecolor = getStringColor(255*axx.Color); - ax.range = eval(sprintf('axisData.%sLim', axName)); + ax.range = axisData.(axName + "Lim"); %-label-% - label = eval(sprintf('axisData.%sLabel', axName)); + label = axisData.(axName + "Label"); ax.title = label.String; if ~isempty(ax.title), ax.title = parseString(ax.title); end ax.titlefont.size = label.FontSize; @@ -194,7 +194,7 @@ function updateScene(obj, dataIndex) end %-grid-% - axGrid = eval(sprintf('axisData.%sGrid', axName)); + axGrid = axisData.(axName + "Grid"); if strcmp(axGrid, 'off'), ax.showgrid = false; end %-box-% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateImage.m b/plotly/plotlyfig_aux/handlegraphics/updateImage.m index 0c9a44f0..c2a624bd 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateImage.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateImage.m @@ -43,8 +43,8 @@ image_data = obj.State.Plot(imageIndex).Handle; %-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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateImage3D.m b/plotly/plotlyfig_aux/handlegraphics/updateImage3D.m index dc268d55..4f2a48ef 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateImage3D.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateImage3D.m @@ -43,8 +43,8 @@ image_data = obj.State.Plot(imageIndex).Handle; %-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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateImplicitFunctionSurface.m b/plotly/plotlyfig_aux/handlegraphics/updateImplicitFunctionSurface.m index b10410dc..9bbacf69 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateImplicitFunctionSurface.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateImplicitFunctionSurface.m @@ -11,8 +11,8 @@ figure_data = obj.State.Figure.Handle; %-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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m b/plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m index a8c48251..ed17adb0 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateIsosurface.m @@ -78,7 +78,7 @@ function updateScene(obj, isoIndex) plotData = obj.State.Plot(isoIndex).Handle; axisData = plotData.Parent; [xSource, ySource] = findSourceAxis(obj, axIndex); - scene = eval( sprintf('obj.layout.scene%d', xSource) ); + scene = obj.layout.("scene" + xSource); aspectRatio = axisData.PlotBoxAspectRatio; cameraPosition = axisData.CameraPosition; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateLineseries.m b/plotly/plotlyfig_aux/handlegraphics/updateLineseries.m index 8d468988..fb5145a9 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateLineseries.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateLineseries.m @@ -113,7 +113,7 @@ function updateScene(obj, dataIndex) plotData = obj.State.Plot(dataIndex).Handle; axisData = plotData.Parent; [xSource, ~] = findSourceAxis(obj, axIndex); - scene = eval( sprintf('obj.layout.scene%d', xSource) ); + scene = obj.layout.("scene" + xSource); cameraTarget = axisData.CameraTarget; position = axisData.Position; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateMesh.m b/plotly/plotlyfig_aux/handlegraphics/updateMesh.m index 55bae1fb..75d8ae36 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateMesh.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateMesh.m @@ -13,7 +13,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; diff --git a/plotly/plotlyfig_aux/handlegraphics/updatePColor.m b/plotly/plotlyfig_aux/handlegraphics/updatePColor.m index 36111ed1..8d84b036 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updatePColor.m +++ b/plotly/plotlyfig_aux/handlegraphics/updatePColor.m @@ -11,8 +11,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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updatePatch.m b/plotly/plotlyfig_aux/handlegraphics/updatePatch.m index bf4d8739..4a72599d 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updatePatch.m +++ b/plotly/plotlyfig_aux/handlegraphics/updatePatch.m @@ -53,8 +53,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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updatePie3.m b/plotly/plotlyfig_aux/handlegraphics/updatePie3.m index 278b0328..e25db4ae 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updatePie3.m +++ b/plotly/plotlyfig_aux/handlegraphics/updatePie3.m @@ -78,7 +78,7 @@ function updatePie3(obj,plotIndex) [xsource, ysource] = findSourceAxis(obj,axIndex); %-AXIS DATA-% - eval(['scene = obj.layout.scene' num2str(xsource) ';']); + scene = obj.layout.("scene" + xsource); obj.PlotOptions.scene_anchor = ['scene' num2str(xsource)]; %-------------------------------------------------------------------------% @@ -230,7 +230,7 @@ function updatePie3(obj,plotIndex) figure_data = obj.State.Figure.Handle; %-AXIS DATA-% - eval(['scene = obj.layout.scene' num2str(xsource) ';']); + scene = obj.layout.("scene" + xsource); obj.PlotOptions.scene_anchor = ['scene' num2str(xsource)]; %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateQuiver.m b/plotly/plotlyfig_aux/handlegraphics/updateQuiver.m index 3ac2bc02..01e0842c 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateQuiver.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateQuiver.m @@ -143,7 +143,7 @@ function updateScene(obj, dataIndex) plotData = obj.State.Plot(dataIndex).Handle; axisData = plotData.Parent; [xSource, ~] = findSourceAxis(obj, axIndex); - scene = eval( sprintf('obj.layout.scene%d', xSource) ); + scene = obj.layout.("scene" + xSource); position = axisData.Position; aspectRatio = axisData.PlotBoxAspectRatio; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateRectangle.m b/plotly/plotlyfig_aux/handlegraphics/updateRectangle.m index 6f7e068c..62769f02 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateRectangle.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateRectangle.m @@ -38,8 +38,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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateScattergroup.m b/plotly/plotlyfig_aux/handlegraphics/updateScattergroup.m index 90c3e097..3dcd0f2f 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateScattergroup.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateScattergroup.m @@ -67,8 +67,8 @@ function updateScattergroup(obj,scatterIndex) [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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateScatterhistogram.m b/plotly/plotlyfig_aux/handlegraphics/updateScatterhistogram.m index 671fcaa6..8d4717ce 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateScatterhistogram.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateScatterhistogram.m @@ -143,9 +143,9 @@ function updateMainScatterAxis(obj, plotIndex) axisPos = plotData.Position; axisColor = 'rgba(0,0,0, 0.9)'; - axisLim = eval(sprintf('plotData.%sLimits', axName)); - axisPlot = eval(sprintf('plotData.%sData', axName)); - axisLabel = eval(sprintf('plotData.%sLabel', axName)); + axisLim = plotData.(axName + "Limits"); + axisPlot = plotData.(axName + "Data"); + axisLabel = plotData.(axName + "Label"); switch axName case 'X' @@ -489,11 +489,11 @@ function updateYMarginalAxis(obj, plotIndex) end function axisLim = getAxisLim(plotData, axName) - axisLim = eval(sprintf('plotData.%sLimits', axName)); - axisPlot = eval(sprintf('plotData.%sData', axName)); + axisLim = plotData.(axName + "Limits"); + axisPlot = plotData.(axName + "Data"); if iscategorical(axisPlot) - axisPlot = eval(sprintf('plotData.%sData', axName)); + axisPlot = plotData.(axName + "Data"); [~, ~, axisPlot] = unique(axisPlot); axisLim = [min(axisPlot)-0.5, max(axisPlot)+0.5]; end @@ -541,7 +541,7 @@ function updateTitle(obj, plotIndex) if isTitle titleText = sprintf('%s', parseString(plotData.Title)); titleFamily = matlab2plotlyfont(plotData.FontName); - xaxis = eval(sprintf('obj.layout.xaxis%d', xSource)); + xaxis = obj.layout.("xaxis" + xSource); obj.layout.annotations{1}.text = titleText; obj.layout.annotations{1}.x = mean(xaxis.domain); diff --git a/plotly/plotlyfig_aux/handlegraphics/updateSlice.m b/plotly/plotlyfig_aux/handlegraphics/updateSlice.m index 7a893d5f..bb6d37e8 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateSlice.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateSlice.m @@ -82,7 +82,7 @@ function updateScene(obj, dataIndex) plotData = obj.State.Plot(dataIndex).Handle; axisData = plotData.Parent; [xSource, ~] = findSourceAxis(obj, axIndex); - scene = eval( sprintf('obj.layout.scene%d', xSource) ); + scene = obj.layout.("scene" + xSource); aspectRatio = axisData.PlotBoxAspectRatio; cameraPosition = axisData.CameraPosition; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateStackedplot.m b/plotly/plotlyfig_aux/handlegraphics/updateStackedplot.m index 72c1f389..9157ef1b 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateStackedplot.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateStackedplot.m @@ -331,8 +331,8 @@ function updateStackedplotAxis(obj, plotIndex) end function updateTitle(obj, titleText, xySource) - xaxis = eval(sprintf('obj.layout.xaxis%d', xySource(1))); - yaxis = eval(sprintf('obj.layout.yaxis%d', xySource(2))); + xaxis = obj.layout.("xaxis" + xySource(1)); + yaxis = obj.layout.("yaxis" + xySource(2)); anIndex = 1; if ~isempty(titleText), titleText = parseString(titleText); end @@ -354,8 +354,8 @@ function updateTitle(obj, titleText, xySource) function updateExponentFormat(obj, expoFormat, xySource, axName) axName = lower(axName); - xaxis = eval(sprintf('obj.layout.xaxis%d', xySource(1))); - yaxis = eval(sprintf('obj.layout.yaxis%d', xySource(2))); + xaxis = obj.layout.("xaxis" + xySource(1)); + yaxis = obj.layout.("yaxis" + xySource(2)); anIndex = obj.PlotlyDefaults.anIndex + 1; if expoFormat ~= 0 diff --git a/plotly/plotlyfig_aux/handlegraphics/updateStreamtube.m b/plotly/plotlyfig_aux/handlegraphics/updateStreamtube.m index 0400057e..b6023756 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateStreamtube.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateStreamtube.m @@ -17,8 +17,8 @@ function updateSurfaceStreamtube(obj, surfaceIndex) figure_data = obj.State.Figure.Handle; %-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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateSurf.m b/plotly/plotlyfig_aux/handlegraphics/updateSurf.m index e37cea40..ac213aa5 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateSurf.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateSurf.m @@ -13,7 +13,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; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateSurfaceplot.m b/plotly/plotlyfig_aux/handlegraphics/updateSurfaceplot.m index cdf97f3c..b1e383cd 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateSurfaceplot.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateSurfaceplot.m @@ -11,8 +11,8 @@ figure_data = obj.State.Figure.Handle; %-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); %-------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateSurfc.m b/plotly/plotlyfig_aux/handlegraphics/updateSurfc.m index cb7b5d64..6a8656be 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateSurfc.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateSurfc.m @@ -165,7 +165,7 @@ function updateSurfOnly(obj, surfaceIndex) 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; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateSurfl.m b/plotly/plotlyfig_aux/handlegraphics/updateSurfl.m index bd2dd4e4..46c998b8 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateSurfl.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateSurfl.m @@ -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; diff --git a/plotly/plotlyfig_aux/handlegraphics/updateTernaryContour.m b/plotly/plotlyfig_aux/handlegraphics/updateTernaryContour.m index b2501c27..a9ed9c19 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateTernaryContour.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateTernaryContour.m @@ -382,17 +382,16 @@ function ternaryAxes(obj, ternaryIndex) for l = 1:length(labelIndex) n = labelIndex(l); - patterText = sprintf('ternary.%saxis.title', labelLetter{l}); labelText = axisData.Children(n).String; labelFontColor = sprintf('rgb(%f,%f,%f)', axisData.Children(n).Color); labelFontSize = 1.5 * axisData.Children(n).FontSize; labelFontFamily = matlab2plotlyfont(axisData.Children(n).FontName); - eval(sprintf('%s.text = labelText;', patterText)); - eval(sprintf('%s.font.color = labelFontColor;', patterText)); - eval(sprintf('%s.font.size = labelFontColor;', patterText)); - eval(sprintf('%s.font.family = labelFontFamily;', patterText)); + ternary.(labelLetter(l) + "axis").title.text = labelText; + ternary.(labelLetter(l) + "axis").title.font.color = labelFontColor; + ternary.(labelLetter(l) + "axis").title.font.size = labelFontSize; + ternary.(labelLetter(l) + "axis").title.font.family = labelFontFamily; end %-----------------------------------------------------------------------------% @@ -408,13 +407,11 @@ function ternaryAxes(obj, ternaryIndex) tickFontFamily = matlab2plotlyfont(axisData.Children(t0).FontName); for l = 1:3 - patterText = sprintf('ternary.%saxis', labelLetter{l}); - - eval(sprintf('%s.tick0 = tick0;', patterText)); - eval(sprintf('%s.dtick = dtick;', patterText)); - eval(sprintf('%s.tickfont.color = tickFontColor;', patterText)); - eval(sprintf('%s.tickfont.size = tickFontSize;', patterText)); - eval(sprintf('%s.tickfont.family = tickFontFamily;', patterText)); + ternary.(labelLetter{l} + "axis").tick0 = tick0; + ternary.(labelLetter{l} + "axis").dtick = dtick; + ternary.(labelLetter{l} + "axis").tickfont.color = tickFontColor; + ternary.(labelLetter{l} + "axis").tickfont.size = tickFontSize; + ternary.(labelLetter{l} + "axis").tickfont.family = tickFontFamily; end %-----------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlot.m b/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlot.m index ca23cec3..a214fe7e 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlot.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlot.m @@ -110,17 +110,16 @@ for l = 1:length(labelIndex) n = labelIndex(l); - patterText = sprintf('ternary.%saxis.title', labelLetter{l}); labelText = axisData.Children(n).String; labelFontColor = sprintf('rgb(%f,%f,%f)', axisData.Children(n).Color); labelFontSize = 1.5 * axisData.Children(n).FontSize; labelFontFamily = matlab2plotlyfont(axisData.Children(n).FontName); - eval(sprintf('%s.text = labelText;', patterText)); - eval(sprintf('%s.font.color = labelFontColor;', patterText)); - eval(sprintf('%s.font.size = labelFontColor;', patterText)); - eval(sprintf('%s.font.family = labelFontFamily;', patterText)); + ternary.(labelLetter{l} + "axis").title.text = labelText; + ternary.(labelLetter{l} + "axis").title.font.color = labelFontColor; + ternary.(labelLetter{l} + "axis").title.font.size = labelFontSize; + ternary.(labelLetter{l} + "axis").title.font.family = labelFontFamily; end %-----------------------------------------------------------------------------% @@ -136,13 +135,11 @@ tickFontFamily = matlab2plotlyfont(axisData.Children(t0).FontName); for l = 1:3 - patterText = sprintf('ternary.%saxis', labelLetter{l}); - - eval(sprintf('%s.tick0 = tick0;', patterText)); - eval(sprintf('%s.dtick = dtick;', patterText)); - eval(sprintf('%s.tickfont.color = tickFontColor;', patterText)); - eval(sprintf('%s.tickfont.size = tickFontSize;', patterText)); - eval(sprintf('%s.tickfont.family = tickFontFamily;', patterText)); + ternary.(labelLetter{l} + "axis").tick0 = tick0; + ternary.(labelLetter{l} + "axis").dtick = dtick; + ternary.(labelLetter{l} + "axis").tickfont.color = tickFontColor; + ternary.(labelLetter{l} + "axis").tickfont.size = tickFontSize; + ternary.(labelLetter{l} + "axis").tickfont.family = tickFontFamily; end %-----------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlotPro.m b/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlotPro.m index 5ab2b16f..38c84c79 100644 --- a/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlotPro.m +++ b/plotly/plotlyfig_aux/handlegraphics/updateTernaryPlotPro.m @@ -170,17 +170,16 @@ for l = 1:length(labelIndex) n = labelIndex(l); - patterText = sprintf('ternary.%saxis.title', labelLetter{l}); labelText = axisData.Children(n).String; labelFontColor = sprintf('rgb(%f,%f,%f)', axisData.Children(n).Color); labelFontSize = 1.5 * axisData.Children(n).FontSize; labelFontFamily = matlab2plotlyfont(axisData.Children(n).FontName); - eval(sprintf('%s.text = labelText;', patterText)); - eval(sprintf('%s.font.color = labelFontColor;', patterText)); - eval(sprintf('%s.font.size = labelFontColor;', patterText)); - eval(sprintf('%s.font.family = labelFontFamily;', patterText)); + ternary.(labelLetter{l} + "axis").title.text = labelText; + ternary.(labelLetter{l} + "axis").title.font.color = labelFontColor; + ternary.(labelLetter{l} + "axis").title.font.size = labelFontSize; + ternary.(labelLetter{l} + "axis").title.font.family = labelFontFamily; end %-----------------------------------------------------------------------------% @@ -196,13 +195,11 @@ tickFontFamily = matlab2plotlyfont(axisData.Children(t0).FontName); for l = 1:3 - patterText = sprintf('ternary.%saxis', labelLetter{l}); - - eval(sprintf('%s.tick0 = tick0;', patterText)); - eval(sprintf('%s.dtick = dtick;', patterText)); - eval(sprintf('%s.tickfont.color = tickFontColor;', patterText)); - eval(sprintf('%s.tickfont.size = tickFontSize;', patterText)); - eval(sprintf('%s.tickfont.family = tickFontFamily;', patterText)); + ternary.(labelLetter{l} + "axis").tick0 = tick0; + ternary.(labelLetter{l} + "axis").dtick = dtick; + ternary.(labelLetter{l} + "axis").tickfont.color = tickFontColor; + ternary.(labelLetter{l} + "axis").tickfont.size = tickFontSize; + ternary.(labelLetter{l} + "axis").tickfont.family = tickFontFamily; end %-----------------------------------------------------------------------------% diff --git a/plotly/plotlyfig_aux/helpers/extractHeatmapAxisData.m b/plotly/plotlyfig_aux/helpers/extractHeatmapAxisData.m index e70ba08d..5e09c149 100644 --- a/plotly/plotlyfig_aux/helpers/extractHeatmapAxisData.m +++ b/plotly/plotlyfig_aux/helpers/extractHeatmapAxisData.m @@ -36,7 +36,7 @@ %-------------------------------------------------------------------------% -tl = eval(['axis_data.' axisName 'Data;']); +tl = axis_data.(axisName + "Data"); tl = length(tl); w = axis_data.Position(4); @@ -84,8 +84,8 @@ axis.ticks = 'inside'; axis.mirror = true; -labels = eval(['axis_data.' axisName 'DisplayLabels;']); -vals = eval(['axis_data.' axisName 'DisplayData;']); +labels = axis_data.(axisName + "DisplayLabels"); +vals = axis_data.(axisName + "DisplayData"); axis.showticklabels = true; axis.type = 'category'; @@ -97,7 +97,7 @@ %-------------------------------LABELS------------------------------------% -label = eval(['axis_data.' axisName 'Label;']); +label = axis_data.(axisName + "Label"); %-------------------------------------------------------------------------% From a921e183e90dcd8423f9518c105ed00d5a6a216c Mon Sep 17 00:00:00 2001 From: robertoffmoura Date: Mon, 5 Aug 2024 11:43:51 +0100 Subject: [PATCH 2/4] Use string array instead of string cell --- plotly/plotlyfig_aux/helpers/extractLineMarker.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plotly/plotlyfig_aux/helpers/extractLineMarker.m b/plotly/plotlyfig_aux/helpers/extractLineMarker.m index c4a4e7fd..6c53f35f 100644 --- a/plotly/plotlyfig_aux/helpers/extractLineMarker.m +++ b/plotly/plotlyfig_aux/helpers/extractLineMarker.m @@ -64,8 +64,8 @@ %---------------------------------------------------------------------% - filledMarkerSet = {"o","square","s","diamond","d",... - "v","^", "<",">","hexagram","pentagram"}; + filledMarkerSet = ["o","square","s","diamond","d",... + "v","^", "<",">","hexagram","pentagram"]; filledMarker = ismember(line_data.Marker,filledMarkerSet); From 5b1b0f10748eb1080f526d8091c713e0c4dc1f40 Mon Sep 17 00:00:00 2001 From: robertoffmoura Date: Mon, 5 Aug 2024 13:48:19 +0100 Subject: [PATCH 3/4] Rewrite struct2json and cell2json --- plotly/plotly_aux/cell2json.m | 12 +++--------- plotly/plotly_aux/struct2json.m | 10 ++-------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/plotly/plotly_aux/cell2json.m b/plotly/plotly_aux/cell2json.m index 5f8ca90a..2047dddb 100644 --- a/plotly/plotly_aux/cell2json.m +++ b/plotly/plotly_aux/cell2json.m @@ -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 \ No newline at end of file + strList = string(cellfun(@m2json, s, un=0)); + str = sprintf("[%s]", strjoin(strList, ", ")); +end diff --git a/plotly/plotly_aux/struct2json.m b/plotly/plotly_aux/struct2json.m index 6c6eb4ce..6555a7f0 100644 --- a/plotly/plotly_aux/struct2json.m +++ b/plotly/plotly_aux/struct2json.m @@ -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 \ No newline at end of file From ba5752015922b8646d7b88b116837a792e945aa0 Mon Sep 17 00:00:00 2001 From: robertoffmoura Date: Mon, 5 Aug 2024 15:03:19 +0100 Subject: [PATCH 4/4] Add m2json test cases, fix convertDate --- plotly/plotly_aux/Test_m2json.m | 67 ++++++++++++++++++++-- plotly/plotly_aux/m2json.m | 20 ++++--- plotly/plotlyfig_aux/helpers/convertDate.m | 5 +- 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/plotly/plotly_aux/Test_m2json.m b/plotly/plotly_aux/Test_m2json.m index 8cb2d0d2..11f08fd0 100644 --- a/plotly/plotly_aux/Test_m2json.m +++ b/plotly/plotly_aux/Test_m2json.m @@ -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 @@ -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 diff --git a/plotly/plotly_aux/m2json.m b/plotly/plotly_aux/m2json.m index f9f9b309..faa68089 100644 --- a/plotly/plotly_aux/m2json.m +++ b/plotly/plotly_aux/m2json.m @@ -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)); @@ -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"); @@ -55,7 +62,6 @@ end end -function x = clip(x,lb,ub) - x(xub) = ub; +function numDigits = getPrecision(val) + numDigits = strlength(sprintf("%.15g", val)); end diff --git a/plotly/plotlyfig_aux/helpers/convertDate.m b/plotly/plotlyfig_aux/helpers/convertDate.m index 0e2f7374..acae4c40 100644 --- a/plotly/plotlyfig_aux/helpers/convertDate.m +++ b/plotly/plotlyfig_aux/helpers/convertDate.m @@ -1,15 +1,16 @@ function output = convertDate(date) date = convertToDateTime(date); if isDate(date) - format = "yyyy-mm-dd"; + format = "yyyy-MM-dd"; else - format = "yyyy-mm-dd HH:MM:ss"; + format = "yyyy-MM-dd HH:mm:ss"; end output = string(date, format); end function dt = convertToDateTime(input) if isdatetime(input) + dt = input; return elseif isnumeric(input) % Assume input is a datenum