Files
imdd_silas/Classes/Warehouse_class/classes/DataStorage2.m
Silas Labor Zizou 6b0f9de118 Lab changes
2024-10-10 11:07:59 +02:00

115 lines
4.2 KiB
Matlab

classdef DataStorage2 < handle
% DATASTORAGE: Stores data with physical parameter mappings
properties
inputParams = struct;
parameter = struct;
fn = [];
dim = [];
sto = struct;
end
methods
function obj = DataStorage2(inputParams)
% Constructor to initialize the DataStorage object
if nargin > 0
obj.inputParams = inputParams;
obj.fn = string(fieldnames(inputParams));
obj = obj.buildParameter();
obj.dim = obj.getDimension();
obj.sto = struct;
else
error('Input parameters are required.');
end
end
function showInfo(obj)
% Displays information about the storage and its dimensions
disp("Data Structure with fields:");
fprintf('%-12s | %-8s | %-12s\n', 'Name', 'Dimension', 'Physical Values');
disp('-------------------------------------------------------');
for i = 1:numel(obj.fn)
fprintf('%-12s | %-8d | %-12s\n', ...
char(obj.fn(i)), obj.dim(i), ...
strjoin(string(obj.parameter.(obj.fn(i)).values), ', '));
end
disp('-------------------------------------------------------');
end
function dim = getDimension(obj)
% Get the dimensions based on the length of parameters
dim = zeros(1, numel(obj.fn));
for p = 1:numel(obj.fn)
dim(p) = obj.parameter.(obj.fn(p)).length;
end
end
function obj = buildParameter(obj)
% Build the Parameter objects for each input parameter
for p = 1:numel(obj.fn)
name = obj.fn(p);
values = obj.inputParams.(name);
obj.parameter.(name) = Parameter2(name, values);
end
end
function addStorage(obj, varName)
% Create an empty storage for a specific variable name
obj.sto.(string(varName)) = cell(obj.dim);
end
function addValueToStorage(obj, valueToStore, storageVarName, varargin)
% Add a value to the storage at the specified indices
if nargin - 3 == numel(obj.fn)
lin_idx = obj.getIndicesByPhys(varargin);
obj.sto.(storageVarName){lin_idx} = valueToStore;
else
error('Please provide all indices for the storage.');
end
end
function value = getStoValue(obj, storageVarName, varargin)
% Retrieve a value from storage based on physical parameters
if nargin - 2 == numel(obj.fn)
lin_idx = obj.getIndicesByPhys(varargin);
value = cell(1, numel(lin_idx));
for i = 1:numel(lin_idx)
value{i} = obj.sto.(storageVarName){lin_idx(i)};
end
value = value(~cellfun('isempty', value)); % Remove empty entries
else
error('Please provide all physical parameters.');
end
end
function lin_idx = getIndicesByPhys(obj, varargin)
% Unpack nested cell array if needed
if numel(varargin) == 1 && iscell(varargin{1})
varargin = varargin{1}; % Unpack if single cell array is passed
end
indices = cell(1, numel(obj.fn));
% Loop through each parameter (e.g., L, D)
for p = 1:numel(obj.fn)
% Unwrap if it's a cell
if iscell(varargin{p})
physVal = varargin{p}{1}; % Extract scalar from cell
else
physVal = varargin{p}; % It's already a scalar
end
paramName = obj.fn(p); % Get the parameter name (e.g., 'L' or 'D')
% Call getIndexByPhys on the corresponding Parameter2 object
indices{p} = obj.parameter.(paramName).getIndexByPhys(physVal);
end
% Convert subscript indices to a linear index
lin_idx = sub2ind(obj.dim, indices{:});
end
end
end