*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects
Commit 3d1e8db6 authored by Innerberger, Michael's avatar Innerberger, Michael
Browse files

Use argument blocks in get/set methods

parent acd69fd6
No related branches found
No related tags found
No related merge requests found
...@@ -18,9 +18,10 @@ classdef LevelData < handle ...@@ -18,9 +18,10 @@ classdef LevelData < handle
% %
properties properties
% map for general metadata
metaData metaData
% Root folder for file storage % Root folder for file storage
root = 'results' root
% Structure array storing string representations of variables % Structure array storing string representations of variables
dictionary dictionary
% Cell array of level data % Cell array of level data
...@@ -79,10 +80,10 @@ classdef LevelData < handle ...@@ -79,10 +80,10 @@ classdef LevelData < handle
%file storage is initialised in the specified optional path %file storage is initialised in the specified optional path
% leveldata = LEVELDATA(rootpath) % leveldata = LEVELDATA(rootpath)
% Set root path for file storage arguments
if nargin >= 1 rootpath {mustBeTextScalar} = 'results'
obj.root = rootpath;
end end
obj.root = rootpath;
% TODO: set output of hostname to string and remove cast % TODO: set output of hostname to string and remove cast
obj.metaData = dictionary(... obj.metaData = dictionary(...
...@@ -106,7 +107,10 @@ classdef LevelData < handle ...@@ -106,7 +107,10 @@ classdef LevelData < handle
%% SET GLOBAL VARIABLES %% SET GLOBAL VARIABLES
function set.root(obj, path) function set.root(obj, path)
assert(ischar(path), 'Insert character array as path name.'); arguments
obj
path {mustBeTextScalar}
end
obj.root = path; obj.root = path;
end end
...@@ -221,7 +225,7 @@ classdef LevelData < handle ...@@ -221,7 +225,7 @@ classdef LevelData < handle
end end
%% READ LEVEL DATA %% READ LEVEL DATA
data = get(obj, jLevel, varargin) data = get(obj, jLevel, variableName)
%% MODIFY LEVEL DATA %% MODIFY LEVEL DATA
set(obj, jLevel, varargin) set(obj, jLevel, varargin)
......
function data = get(obj, jLevel, varargin) function data = get(obj, jLevel, variableName)
%%GET extracts data from this LevelData object on a given list jLevel of %%GET extracts data from this LevelData object on a given list jLevel of
%level numbers, the cell array varargin must contain the names of the %level numbers. One or more variables to be returned can be specified by their
%variables to return %names.
% data = GET(obj, jLevel, varargin) % data = GET(obj, jLevel, variableName, ...)
% data = GET(obj, ':', varargin) % data = GET(obj, ':', variableName, ...)
% Copyright 2023 Philipp Bringmann % Copyright 2023 Philipp Bringmann
% %
...@@ -21,48 +21,58 @@ function data = get(obj, jLevel, varargin) ...@@ -21,48 +21,58 @@ function data = get(obj, jLevel, varargin)
% along with this program. If not, see <http://www.gnu.org/licenses/>. % along with this program. If not, see <http://www.gnu.org/licenses/>.
% %
arguments
obj
jLevel {mustBeIndexVector} = ':'
end
arguments (Repeating)
variableName {mustBeTextScalar}
end
% Proceed input
if strcmp(jLevel, ':') if strcmp(jLevel, ':')
jLevel = 1:obj.nLevel; jLevel = 1:obj.nLevel;
end end
% Initialise return variable % Initialise return variable
data = nan(length(jLevel), length(varargin)); data = nan(length(jLevel), length(variableName));
containsCharOnly = true; containsCharOnly = true;
% filter non-existing variables
existingVariables = ismember(variableName, obj.label);
if any(~existingVariables)
warning(['Variable(s) ', strjoin(variableName(~existingVariables), ', '), ' not found']);
end
variableName = variableName(existingVariables);
% Iterate over all variables % Iterate over all variables
for jVariable = 1:length(varargin) for jVariable = 1:length(variableName)
variableName = varargin{jVariable}; name = variableName{jVariable};
% Check for existence of specified variable
if ~ismember(variableName, obj.label) % Check for strings/character arrays
warning(['Variable ', variableName, ' not found']); if ~strcmp(obj.type.(name).type, 'c') && ...
value = []; ~strcmp(obj.type.(name).type, 's')
else containsCharOnly = false;
% Check for strings/character arrays end
if ~strcmp(obj.type.(variableName).type, 'c') && ... % Determine index of current variable
~strcmp(obj.type.(variableName).type, 's') idx = obj.getIndex(name);
containsCharOnly = false; if obj.isScalar(idx)
% Extract scalar variables for each level
value = zeros(length(jLevel), 1);
for k = 1:length(jLevel)
value(k) = obj.level(jLevel(k)).(name);
end end
% Determine index of current variable else
idx = obj.getIndex(variableName); % Extract non-scalar variables only in case of a single level
if obj.isScalar(idx) if length(jLevel) > 1
% Extract scalar variables for each level warning('Export of level-oriented scalar variables only');
value = zeros(length(jLevel), 1); value = [];
for k = 1:length(jLevel)
value(k) = obj.level(jLevel(k)).(variableName);
end
else else
% Extract non-scalar variables only in case of a single level data(1,jVariable) = obj.level(jLevel).(name);
if length(jLevel) > 1 return
warning('Export of level-oriented scalar variables only');
value = [];
else
data(1,jVariable) = obj.level(jLevel).(variableName);
return
end
end end
end end
% Save extracted data to return variable % Save extracted data to return variable
data(:,jVariable) = value; data(:,jVariable) = value;
% Post-process character arrays % Post-process character arrays
......
function set(obj, jLevel, varargin) function set(obj, jLevel, variableName, value)
%%SET stores specified data to this LevelData object for the specified list %%SET stores specified data to this LevelData object for the specified list
%jLevel of levels, the cell array varargin must contain pairs of variable %jLevel of levels. The variableName/value pairs can be repeating, their first
%names and data (numeric or string/characters), dimension one of data must %dimension must correspond to number of levels
%correspond to number of levels % SET(obj, jLevel, variableName, value, ...)
% SET(obj, jLevel, varargin) % SET(obj, ':', variableName, value, ...)
% SET(obj, ':', varargin)
% Copyright 2023 Philipp Bringmann % Copyright 2023 Philipp Bringmann
% %
...@@ -22,41 +21,44 @@ function set(obj, jLevel, varargin) ...@@ -22,41 +21,44 @@ function set(obj, jLevel, varargin)
% along with this program. If not, see <http://www.gnu.org/licenses/>. % along with this program. If not, see <http://www.gnu.org/licenses/>.
% %
arguments
obj
jLevel {mustBeIndexVector} = ':'
end
arguments (Repeating)
variableName {mustBeTextScalar}
value
end
% Proceed input
if strcmp(jLevel, ':') if strcmp(jLevel, ':')
jLevel = 1:obj.nLevel; jLevel = 1:obj.nLevel;
end end
% Check number of variable input
assert(mod(length(varargin), 2) == 0, ...
'Invalid number of arguments');
% Determine number of arguments % Determine number of arguments
nArgument = length(varargin) / 2; nArgument = length(variableName);
% Store data to LevelData object % Store data to LevelData object
for j = 1:nArgument for j = 1:nArgument
variableName = varargin{2*j-1}; name = variableName{j};
val = value{j};
% Determine type of data % Determine type of data
if isa(varargin{2*j}, 'Type') if isa(val, 'Type')
% Copy type argument % Copy type argument
currentType = varargin{2*j}; currentType = val;
valueList = repmat({nan}, length(jLevel), 1); valueList = repmat({nan}, length(jLevel), 1);
else else
% Determine type by example data % Determine type by example data
[currentType, valueList] = ... [currentType, valueList] = ...
Type.determineTypeValue(length(jLevel), ... Type.determineTypeValue(length(jLevel), name, val);
variableName, ...
varargin{2*j});
end end
% Store type % Store type
if ~ismember(variableName, obj.label) if ~ismember(name, obj.label)
obj.type.(variableName) = currentType; obj.type.(name) = currentType;
end end
% Store level-oriented data % Store level-oriented data
for k = 1:length(jLevel) for k = 1:length(jLevel)
obj.level(jLevel(k)).(variableName) = valueList{k}; obj.level(jLevel(k)).(name) = valueList{k};
end end
end end
end end
\ No newline at end of file
function setAbsolute(obj, jLevel, varargin) function setAbsolute(obj, jLevel, variableName, value)
%%SETABSOLUTE stores specified data of absolute-niveau type (constants / %%SETABSOLUTE stores specified data of absolute-niveau type (constants /
%eigenvalues / efficiency indices / ...) to this %eigenvalues / efficiency indices / ...) to this LevelData object for the
%LevelData object for the specified list jLevel of levels, the cell array %specified list jLevel of levels, repeated pairs of variable names and values,
%varargin must contain pairs of variable names and data, dimension one of %first dimension of data must correspond to number of levels
%data must correspond to number of levels % SETABSOLUTE(obj, jLevel, variableName, value, ...)
% SETABSOLUTE(obj, jLevel, varargin) % SETABSOLUTE(obj, ':', variableName, value, ...)
% SETABSOLUTE(obj, ':', varargin)
% %
% See also LevelData/set % See also LevelData/set
...@@ -25,45 +24,47 @@ function setAbsolute(obj, jLevel, varargin) ...@@ -25,45 +24,47 @@ function setAbsolute(obj, jLevel, varargin)
% along with this program. If not, see <http://www.gnu.org/licenses/>. % along with this program. If not, see <http://www.gnu.org/licenses/>.
% %
arguments
obj
jLevel {mustBeIndexVector} = ':'
end
arguments (Repeating)
variableName {mustBeTextScalar}
value
end
% Proceed input
if strcmp(jLevel, ':') if strcmp(jLevel, ':')
jLevel = 1:obj.nLevel; jLevel = 1:obj.nLevel;
end end
% Check number of variable input
assert(mod(length(varargin), 2) == 0, ...
'Invalid number of arguments');
% Determine number of arguments % Determine number of arguments
nArgument = length(varargin) / 2; nArgument = length(variableName);
% Store data to LevelData object % Store data to LevelData object
for j = 1:nArgument for j = 1:nArgument
variableName = varargin{2*j-1}; name = variableName{j};
val = value{j};
% Determine type of data % Determine type of data
if isa(varargin{2*j}, 'Type') if isa(val, 'Type')
% Copy type argument % Copy type argument
currentType = varargin{2*j}; currentType = val;
valueList = repmat({nan}, length(jLevel), 1); valueList = repmat({nan}, length(jLevel), 1);
else else
% Determine type by example data % Determine type by example data
[currentType, valueList] = ... [currentType, valueList] = ...
Type.determineTypeValue(length(jLevel), ... Type.determineTypeValue(length(jLevel), name, val);
variableName, ...
varargin{2*j});
end
% Store type
if ~ismember(variableName, obj.absoluteVariable)
obj.type.(variableName) = currentType;
end end
% Store variable as absolute variable
if ~ismember(variableName, obj.absoluteVariable) if ~ismember(name, obj.absoluteVariable)
obj.absoluteVariable{end+1} = variableName; % Store type
obj.type.(name) = currentType;
% Store variable as absolute variable
obj.absoluteVariable{end+1} = name;
end end
% Store level-oriented data % Store level-oriented data
for k = 1:length(jLevel) for k = 1:length(jLevel)
obj.level(jLevel(k)).(variableName) = valueList{k}; obj.level(jLevel(k)).(name) = valueList{k};
end end
end end
end end
\ No newline at end of file
function setTime(obj, jLevel, varargin) function setTime(obj, jLevel, variableName, value)
%%SETTIME stores specified data of increasing type (time / ndof / ...) to %%SETTIME stores specified data of increasing type (time / ndof / ...) to
%this LevelData object for the specified list jLevel of levels, the cell %this LevelData object for the specified list jLevel of levels, repeated pairs
%array varargin must contain pairs of variable names and data, dimension %of variable names and values, first dimension of data must correspond to number
%one of data must correspond to number of levels %of levels
% SETTIME(obj, jLevel, varargin) % SETTIME(obj, jLevel, variableName, value, ...)
% SETTIME(obj, ':', varargin) % SETTIME(obj, ':', variableName, value, ...)
% %
% See also LevelData/set % See also LevelData/set
...@@ -24,45 +24,48 @@ function setTime(obj, jLevel, varargin) ...@@ -24,45 +24,48 @@ function setTime(obj, jLevel, varargin)
% along with this program. If not, see <http://www.gnu.org/licenses/>. % along with this program. If not, see <http://www.gnu.org/licenses/>.
% %
arguments
obj
jLevel {mustBeIndexVector} = ':'
end
arguments (Repeating)
variableName {mustBeTextScalar}
value
end
% Proceed input
if strcmp(jLevel, ':') if strcmp(jLevel, ':')
jLevel = 1:obj.nLevel; jLevel = 1:obj.nLevel;
end end
% Check number of variable input
assert(mod(length(varargin), 2) == 0, ...
'Invalid number of arguments');
% Determine number of arguments % Determine number of arguments
nArgument = length(varargin) / 2; nArgument = length(variableName);
% Store data to LevelData object % Store data to LevelData object
for j = 1:nArgument for j = 1:nArgument
variableName = varargin{2*j-1}; name = variableName{j};
val = value{j};
% Determine type of data % Determine type of data
if isa(varargin{2*j}, 'Type') if isa(val, 'Type')
% Copy type argument % Copy type argument
currentType = varargin{2*j}; currentType = val;
valueList = repmat({nan}, length(jLevel), 1); valueList = repmat({nan}, length(jLevel), 1);
else else
% Determine type by example data % Determine type by example data
[currentType, valueList] = ... [currentType, valueList] = ...
Type.determineTypeValue(length(jLevel), ... Type.determineTypeValue(length(jLevel), name, val);
variableName, ...
varargin{2*j});
end end
% Store type % Store type
if ~ismember(variableName, obj.label) if ~ismember(name, obj.label)
obj.type.(variableName) = currentType; obj.type.(name) = currentType;
end end
% Store variable as absolute variable % Store variable as absolute variable
if ~ismember(variableName, obj.timeVariable) if ~ismember(name, obj.timeVariable)
obj.timeVariable{end+1} = variableName; obj.timeVariable{end+1} = name;
end end
% Store level-oriented data % Store level-oriented data
for k = 1:length(jLevel) for k = 1:length(jLevel)
obj.level(jLevel(k)).(variableName) = valueList{k}; obj.level(jLevel(k)).(name) = valueList{k};
end end
end end
end end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment