Lab changes

This commit is contained in:
Silas Labor Zizou
2024-10-10 11:07:59 +02:00
parent 285fca45eb
commit 6b0f9de118
21 changed files with 901 additions and 100 deletions

View File

@@ -48,6 +48,14 @@ classdef DataStorage < handle
end
function save(obj,path)
try
save(path,"obj");
catch
end
end
function showInfo(obj)
disp("Data Structure with fields:");
fprintf('%-12s', 'Name'); fprintf('%1s', '| '); fprintf('%0s ', 'Dimension'); fprintf('%4s', '| '); fprintf('%0s ', 'Physical Values'); fprintf('\n');

View File

@@ -0,0 +1,114 @@
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

View File

@@ -0,0 +1,51 @@
classdef Parameter2 < handle
% PARAMETER2: Represents a physical parameter with mappings between values and indices
properties
name
values
length
physToIndexMap % Rename this from 'getPhysForIndex'
indexToPhysMap % Rename this from 'getIndexForPhys'
end
methods
function obj = Parameter2(name, values)
% Constructor to initialize the Parameter2 object
obj.name = name;
obj.values = values;
obj.length = numel(values);
% Initialize the mappings
obj.physToIndexMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
obj.indexToPhysMap = containers.Map('KeyType', 'double', 'ValueType', 'any');
obj = obj.buildMappings();
end
function obj = buildMappings(obj)
% Build mappings between physical values and indices
for idx = 1:obj.length
obj.indexToPhysMap(idx) = obj.values(idx);
obj.physToIndexMap(obj.values(idx)) = idx;
end
end
function physVal = getPhysForIndex(obj, idx)
% Return the physical value corresponding to the index
if isKey(obj.indexToPhysMap, idx)
physVal = obj.indexToPhysMap(idx);
else
error('Index out of range for parameter %s', obj.name);
end
end
function idx = getIndexByPhys(obj, physVal)
% Return the index corresponding to the physical value
if isKey(obj.physToIndexMap, physVal)
idx = obj.physToIndexMap(physVal);
else
error('Physical value %g not found in parameter %s', physVal, obj.name);
end
end
end
end

View File

@@ -0,0 +1,45 @@
% Define input parameters for the DataStorage2
inputParams.L = [1, 2, 10, 80]; % Length in kilometers
inputParams.D = [16, 17, 18]; % Diameter in millimeters
% Create a DataStorage2 instance with the input parameters
dataStorage = DataStorage2(inputParams); % Using DataStorage2 class
% Display the current information about the data storage structure
dataStorage.showInfo();
% Add a storage variable named 'testStorage'
dataStorage.addStorage('testStorage');
% Add a value (e.g., 100) to the storage at specific physical parameter values
% For example, we store the value 100 at L = 10 km and D = 17 mm
dataStorage.addValueToStorage(100, 'testStorage', 10, 16);
dataStorage.addValueToStorage(100, 'testStorage', 10, 17);
dataStorage.addValueToStorage(100, 'testStorage', 10, 18);
% Retrieve the value from the storage at the same physical parameter values
storedValue = dataStorage.getStoValue('testStorage', 10, 16:18);
disp('Retrieved value from storage:');
disp(storedValue);
% Retrieve another value at a non-existent location (L = 2 km, D = 8 mm)
% This will show how the function handles empty storage entries
nonExistentValue = dataStorage.getStoValue('testStorage', 2, 8);
disp('Retrieved value from empty location:');
disp(nonExistentValue);
% Use the internal mappings to check how physical values map to indices
% Get the linear index for physical values L = 10 km and D = 17 mm
lin_idx = dataStorage.getIndicesByPhys(10, 17);
disp('Linear index for L=10 km and D=17 mm:');
disp(lin_idx);
% Check the reverse mapping: physical value for index 2 of parameter L
physValForIndex = dataStorage.parameter.L.getPhysForIndex(2);
disp('Physical value for index 2 of parameter L:');
disp(physValForIndex);
% Check the mapping: index for physical value D = 21 mm
indexForPhys = dataStorage.parameter.D.getIndexByPhys(21);
disp('Index for physical value D=21 mm:');
disp(indexForPhys);