Skip to content
Snippets Groups Projects
Select Git revision
  • unique-event-function
  • usability-improvements
  • main default protected
  • prp-integration
  • CoefficientCsv
  • ode-tests
  • test-9K
  • stability-bugs
  • archive/benchmark
9 results

Settings.m

Blame
  • Settings.m 3.47 KiB
    classdef Settings < Config & dynamicprops
        
        properties
            currentPath
            ode % for ODE options
        end
    
        properties(Access = protected)
            configName = ''
            variableName
        end
    
        properties(Dependent)
            configPath
            dataPath
        end
        
        methods
            function outputVariables = getConfig(obj)
                fileName = obj.configName;
                filePath = obj.mission.configPath;
    
                if ~isfile(fullfile(filePath, fileName))
                    error(['File not found inside the config folder: %s\n' ...
                        'Check that the correct mission is set in your component ' ...
                        'and the config file exists'], fileName); 
                end
    
                variables = [who(); "variables"];
                run(fileName);
                loadedVariables = who();
                loadedVariables(ismember(loadedVariables, variables)) = [];
    
                outputVariables = struct();
                for i = 1:length(loadedVariables)
                    outputVariables.(loadedVariables{i}) = eval(loadedVariables{i});
                end
            end
    
            function obj = Settings()
                obj.currentPath = fullfile(fileparts(mfilename("fullpath")), '..', 'missions');
                obj.ode.optionsasc1 = odeset('Events', @eventApogee, 'InitialStep', 1);
            end
            
            function path = get.configPath(obj)
                if (obj.name), path = fullfile(obj.currentPath, obj.name, 'config');
                else, path = '';
                end
            end
            
            function path = get.dataPath(obj)
                if (obj.name), path = fullfile(obj.currentPath, obj.name, 'data');
                else, path = '';
                end
            end
        end
        
         methods (Access = protected)
            function loadConfig(obj, varargin)
                % This method loads desired configs into calling component.
                % The component can chose between:
                %   - Only specifying the configName (name of the config file):
                %       The loader will look for a variable named like the
                %       config file.
                %   - Specifying both the configName and the variableName:
                %       Useful for when you want to group multiple small
                %       objects into a larger config file
                %
                % Syntax:
                %   obj.loadConfig()        - To load from a config file
                %   obj.loadConfig(vars)    - To load from given variables
    
                fileName = obj.configName;
                varName = obj.variableName;
                if isempty(varName), varName = strtok(fileName, 'C'); end
    
                switch nargin
                    case 1
                        filePath = obj.mission.configPath;
                        if ~isfile(fullfile(filePath, fileName))
                            error(['File not found inside the config folder: %s\n' ...
                                'Check that the correct mission is set in your component ' ...
                                'and the config file exists'], fileName);
                        end
    
                        run(fileName);
                        configObj = eval(varName);
                    case 2
                        configObj = varargin{end}.(varName);
                    otherwise
                        error('Too many input arguments.')
                end
    
                fields = obj.getProperties('writable');
                for j = 1:size(fields, 2), obj.(fields{j}) = configObj.(fields{j}); end
            end
        end
    
        methods(Static, Access = protected)
            function loadData(), end
        end
    end