From 0656d9f2b38ed51665eba4b76290819c60be816a Mon Sep 17 00:00:00 2001 From: Michael Innerberger <michael.innerberger@asc.tuwien.ac.at> Date: Tue, 8 Aug 2023 14:51:38 -0400 Subject: [PATCH] Change argument handling in plot commands --- lib/storage/@LevelData/plot.m | 31 +++++++++------- lib/storage/@LevelData/plotAbsolute.m | 27 ++++++++------ lib/storage/@LevelData/plotLevel.m | 52 +++++++++++++++------------ lib/storage/@LevelData/plotTime.m | 27 ++++++++------ lib/storage/@LevelData/plotToFile.m | 31 ++++++++-------- 5 files changed, 97 insertions(+), 71 deletions(-) diff --git a/lib/storage/@LevelData/plot.m b/lib/storage/@LevelData/plot.m index 529e2eb..f9e1e0e 100644 --- a/lib/storage/@LevelData/plot.m +++ b/lib/storage/@LevelData/plot.m @@ -1,7 +1,7 @@ -function ax = plot(obj, xVariable, varargin) -%%PLOT plots various scalar variables (specified in varargin) with respect +function ax = plot(obj, xVariable, yVariable) +%%PLOT plots various scalar variables (specified in yVariable) with respect %to the variable xVariable, returns handle to axis object -% ax = PLOT(obj, xVariable, varargin) +% ax = PLOT(obj, xVariable, yVariable, ...) % % See also LevelData/plotTime, LevelData/plotAbsolute @@ -21,22 +21,29 @@ function ax = plot(obj, xVariable, varargin) % along with this program. If not, see <http://www.gnu.org/licenses/>. % + arguments + obj + xVariable {mustBeTextScalar} + end + + arguments (Repeating) + yVariable {mustBeTextScalar} + end - % Proceed input - if nargin >= 3 - variableName = setdiff(varargin, xVariable); - else - variableName = setdiff(obj.label, xVariable); + if isempty(yVariable) + yVariable = setdiff(obj.label, xVariable); end + % TODO: introduce plot specification to allow for different plot types (time, absolute, level) + % Plot only scalar variables that do not belong to time or absolute % variables - variableName = intersect(variableName, obj.scalarVariable); - variableName = setdiff(variableName, obj.absoluteVariable); - variableName = setdiff(variableName, obj.timeVariable); + yVariable = intersect(yVariable, obj.scalarVariable); + yVariable = setdiff(yVariable, obj.absoluteVariable); + yVariable = setdiff(yVariable, obj.timeVariable); % Creates double logarithmic splot - ax = obj.plotLevel('loglog', xVariable, variableName); + ax = obj.plotLevel(@loglog, xVariable, yVariable); % Add title title(ax, 'Convergence history plot'); diff --git a/lib/storage/@LevelData/plotAbsolute.m b/lib/storage/@LevelData/plotAbsolute.m index e94171a..3c57f9f 100644 --- a/lib/storage/@LevelData/plotAbsolute.m +++ b/lib/storage/@LevelData/plotAbsolute.m @@ -1,8 +1,8 @@ -function ax = plotAbsolute(obj, xVariable, varargin) +function ax = plotAbsolute(obj, xVariable, yVariable) %%PLOTABSOLUTE plots various scalar absolute variables (specified in -%varargin) with respect to the variable xVariable, returns handle to axis +%yVariable) with respect to the variable xVariable, returns handle to axis %object -% ax = PLOTABSOLUTE(obj, xVariable, varargin) +% ax = PLOTABSOLUTE(obj, xVariable, yVariable, ...) % % See also LevelData/plot, LevelData/plotTime @@ -22,20 +22,25 @@ function ax = plotAbsolute(obj, xVariable, varargin) % along with this program. If not, see <http://www.gnu.org/licenses/>. % + arguments + obj + xVariable {mustBeTextScalar} + end + + arguments (Repeating) + yVariable {mustBeTextScalar} + end - % Proceed input - if nargin >= 3 - variableName = setdiff(varargin, xVariable); - else - variableName = setdiff(obj.label, xVariable); + if isempty(yVariable) + yVariable = setdiff(obj.label, xVariable); end % Plot only scalar variables that do belong to absolute variables - variableName = intersect(variableName, obj.scalarVariable); - variableName = intersect(variableName, obj.absoluteVariable); + yVariable = intersect(yVariable, obj.scalarVariable); + yVariable = intersect(yVariable, obj.absoluteVariable); % Creates semilogx plot - ax = obj.plotLevel('semilogx', xVariable, variableName); + ax = obj.plotLevel(@semilogx, xVariable, yVariable); % Add title title(ax, 'Value plot'); diff --git a/lib/storage/@LevelData/plotLevel.m b/lib/storage/@LevelData/plotLevel.m index e14537e..e06d1e3 100644 --- a/lib/storage/@LevelData/plotLevel.m +++ b/lib/storage/@LevelData/plotLevel.m @@ -1,9 +1,9 @@ -function ax = plotLevel(obj, plotFunction, xVariable, variableName) +function ax = plotLevel(obj, plotFunction, xVariable, yVariable) %%PLOTLEVEL auxiliary private function for creation of plots for usage in %LevelData/plot, LevelData/plotTime, and LevelData/plotAbsolute, calls the -%specified plotFunction for the plot of variables in variableName with +%specified plotFunction for the plot of variables in yVariable with %respect to xVariable, returns handle to axis object -% ax = PLOTLEVEL(obj, plotFunction, xVariable, variableName) +% ax = PLOTLEVEL(obj, plotFunction, xVariable, yVariable, ...) % Copyright 2023 Philipp Bringmann % @@ -21,6 +21,12 @@ function ax = plotLevel(obj, plotFunction, xVariable, variableName) % along with this program. If not, see <http://www.gnu.org/licenses/>. % + arguments + obj + plotFunction function_handle + xVariable {mustBeTextScalar} + yVariable cell + end % Create handle to currently active axis object ax = gca; @@ -32,25 +38,27 @@ function ax = plotLevel(obj, plotFunction, xVariable, variableName) [COLOURORDER, MARKER] = getPlotStyle(); % Iterate over given variables - for j = 1:length(variableName) - if obj.type.(variableName{j}).isFloat - % Extract value for y-axis - yValue = obj.get(1:obj.nLevel, variableName{j}); - % Extract label for legend from dictionary - if isfield(obj.dictionary, variableName{j}) - variableLabel = obj.dictionary.(variableName{j}); - else - variableLabel = variableName{j}; - end - % Create plot - feval(plotFunction, ... - ax, xValue, yValue, '-', ... - 'Marker', MARKER{mod(j, length(MARKER))}, ... - 'Color', COLOURORDER(j, :), ... - 'LineWidth', 1.5, ... - 'DisplayName', variableLabel); - % Add new line into the current figure when calling plotConvergence again - hold(ax, 'on'); + for j = 1:length(yVariable) + if ~obj.type.(yVariable{j}).isFloat + continue + end + + % Extract value for y-axis + yValue = obj.get(1:obj.nLevel, yVariable{j}); + % Extract label for legend from dictionary + if isfield(obj.dictionary, yVariable{j}) + variableLabel = obj.dictionary.(yVariable{j}); + else + variableLabel = yVariable{j}; end + % Create plot + plotFunction( ... + ax, xValue, yValue, '-', ... + 'Marker', MARKER{mod(j, length(MARKER))}, ... + 'Color', COLOURORDER(j, :), ... + 'LineWidth', 1.5, ... + 'DisplayName', variableLabel); + % Add new line into the current figure when calling plotConvergence again + hold(ax, 'on'); end end \ No newline at end of file diff --git a/lib/storage/@LevelData/plotTime.m b/lib/storage/@LevelData/plotTime.m index 60e78d8..86a80e8 100644 --- a/lib/storage/@LevelData/plotTime.m +++ b/lib/storage/@LevelData/plotTime.m @@ -1,7 +1,7 @@ -function ax = plotTime(obj, xVariable, varargin) -%%PLOTTIME plots various scalar time variables (specified in varargin) with +function ax = plotTime(obj, xVariable, yVariable) +%%PLOTTIME plots various scalar time variables (specified in yVariable) with %respect to the variable xVariable, returns handle to axis object -% ax = PLOTTIME(obj, xVariable, varargin) +% ax = PLOTTIME(obj, xVariable, yVariable, ...) % % See also LevelData/plot, LevelData/plotAbsolute @@ -21,20 +21,25 @@ function ax = plotTime(obj, xVariable, varargin) % along with this program. If not, see <http://www.gnu.org/licenses/>. % + arguments + obj + xVariable {mustBeTextScalar} + end + + arguments (Repeating) + yVariable {mustBeTextScalar} + end - % Proceed input - if nargin >= 3 - variableName = setdiff(varargin, xVariable); - else - variableName = setdiff(obj.label, xVariable); + if isempty(yVariable) + yVariable = setdiff(obj.label, xVariable); end % Plot only scalar variables that do belong to time variables - variableName = intersect(variableName, obj.scalarVariable); - variableName = intersect(variableName, obj.timeVariable); + yVariable = intersect(yVariable, obj.scalarVariable); + yVariable = intersect(yVariable, obj.timeVariable); % Creates double logarithmic splot - ax = obj.plotLevel('loglog', xVariable, variableName); + ax = obj.plotLevel(@loglog, xVariable, yVariable); % Add title title(ax, 'Time plot'); diff --git a/lib/storage/@LevelData/plotToFile.m b/lib/storage/@LevelData/plotToFile.m index 23ffcee..a5fc72f 100644 --- a/lib/storage/@LevelData/plotToFile.m +++ b/lib/storage/@LevelData/plotToFile.m @@ -1,8 +1,8 @@ -function plotToFile(obj, xVariable, varargin) +function plotToFile(obj, xVariable, yVariable) %%PLOTTOFILE creates plots of various scalar variables (specified in -%varargin) with respect %to the variable xVariable and stores it to a file, +%yVariable) with respect %to the variable xVariable and stores it to a file, %filename is generated automatically from information in LevelData object -% PLOTTOFILE(obj, xVariable, varargin) +% PLOTTOFILE(obj, xVariable, yVariable) % % See also LevelData/plot @@ -22,23 +22,24 @@ function plotToFile(obj, xVariable, varargin) % along with this program. If not, see <http://www.gnu.org/licenses/>. % + arguments + obj + xVariable {mustBeTextScalar} + end - % Create problem- and method-specific folder - ensureFolderExists(obj.foldername); - - % Create figure object - h = createStandardFigure(); + arguments (Repeating) + yVariable {mustBeTextScalar} + end - % Create plot - if nargin < 3 - obj.plot(xVariable); - else - obj.plot(xVariable, varargin); + if isempty(yVariable) + yVariable = setdiff(obj.label, xVariable); end + ensureFolderExists(obj.foldername); + h = createStandardFigure(); + obj.plot(xVariable, yVariable{:}); + % Export plot print(h, '-dpng', '-r600', obj.foldername + '/' + obj.filename + '.png'); - - % Close figure close(h); end -- GitLab