Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
LoadFromFile.m
Go to the documentation of this file.
1 %% Eotvos Quantum Transport Utilities - LoadFromFile
2 % Copyright (C) 2009-2016 Peter Rakyta, Ph.D.
3 %
4 % This program is free software: you can redistribute it and/or modify
5 % it under the terms of the GNU General Public License as published by
6 % the Free Software Foundation, either version 3 of the License, or
7 % (at your option) any later version.
8 %
9 % This program is distributed in the hope that it will be useful,
10 % but WITHOUT ANY WARRANTY; without even the implied warranty of
11 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 % GNU General Public License for more details.
13 %
14 % You should have received a copy of the GNU General Public License
15 % along with this program. If not, see http://www.gnu.org/licenses/.
16 %
17 %> @addtogroup basic Basic Functionalities
18 %> @{
19 %> @file LoadFromFile.m
20 %> @brief A class providing interface to load variables from a file.
21 %> @}
22 %> @brief A class providing interface to load variables from a file.
23 %%
24 classdef LoadFromFile < handle
25 
26  properties ( Access = protected )
27  %> structure containing the loaded data
28  LoadedVariable
29  %> the filename
30  filename
31  end
32 
33 methods
34 
35 %% constructorof the class
36 %> @brief Constructor of the class.
37 %> @param filenev The string of the absolute path to the file to open. (Matlab can operate with relative paths as well)
38 %> @return An instance of the class
39 function obj = LoadFromFile( filenev )
40 
41  if exist(filenev, 'file')
42  obj.LoadedVariable = load(filenev);
43  else
44  obj.LoadedVariable = [];
45  end
46 
47  obj.filename = filenev;
48 
49 end
50 
51 %% LoadVariable
52 %> @brief Loads a variable from the opened file.
53 %> @param varname The string containing the name of the variable.
54 %> @param varargin Cell array of optional parameters (https://www.mathworks.com/help/matlab/ref/varargin.html):
55 %> @param 'NoEmpty' Set 'On' for throw an error when the loaded variable is empty, or 'Off' (default) otherwise.
56 %> @param 'StructName' String containing the name of a structure, when the wanted variable is a field of a structure.
57 function ret = LoadVariable(obj, varname, varargin)
58 
59  p = inputParser;
60  p.addParameter('NoEmpty', 'Off');
61  p.addParameter('StructName', []);
62  p.parse(varargin{:});
63 
64  NoEmpty = p.Results.NoEmpty;
65  StructName = p.Results.StructName;
66 
67  if ~isempty(StructName)
68  dfieldnames = fieldnames(obj.LoadedVariable.(StructName));
69  if ismember(varname, dfieldnames)
70  ret = obj.LoadedVariable.(StructName).(varname);
71  else
72  ret = [];
73  end
74 
75  if isempty(ret) && strcmp(NoEmpty, 'On')
76  error(['"', obj.filename, '" has no element called "',StructName,'.',varname,'", or it is empty.']);
77  end
78 
79  return;
80  end
81 
82 
83 
84  if isfield(obj.LoadedVariable, varname)
85  ret = obj.LoadedVariable.(varname);
86  else
87  ret = [];
88  end
89 
90  if isempty(ret) && strcmp(NoEmpty, 'On')
91  error(['"', obj.filename,'" has no element called "',varname,'", or it is empty.']);
92  end
93 end
94 
95 
96 
97 %% setLoadedVariables
98 %> @brief Sets a loaded variable.
99 %> @param varname The string containing the name of the variable.
100 %> @param value The value to be set.
101 %> @param varargin Cell array of optional parameters (https://www.mathworks.com/help/matlab/ref/varargin.html):
102 %> @param 'NoEmpty' Set 'On' for throw an error when the loaded variable is empty, or 'Off' (default) otherwise.
103 %> @param 'StructName' String containing the name of a structure, when the wanted variable is a field of a structure.
104 function ret = setLoadedVariable( obj, varname, value, varargin )
105  p = inputParser;
106  p.addParameter('NoEmpty', 'Off');
107  p.addParameter('StructName', []);
108  p.parse(varargin{:});
109 
110  NoEmpty = p.Results.NoEmpty;
111  StructName = p.Results.StructName;
112 
113  if ~isempty(StructName)
114  dfieldnames = fieldnames(obj.LoadedVariable.(StructName));
115  if ismember(varname, dfieldnames)
116  obj.LoadedVariable.(StructName).(varname) = value;
117  ret = true;
118  else
119  ret = false;
120  end
121 
122  if ~ret && strcmp(NoEmpty, 'On')
123  error(['"',filenev,'" has no element called "',StructName,'.',varname,'", or it cannot be set.']);
124  end
125 
126  return;
127  end
128 
129 
130 
131  if isfield(obj.LoadedVariable, varname)
132  obj.LoadedVariable.(varname) = value;
133  ret = true;
134  else
135  ret = false;
136  end
137 
138  if ~ret && strcmp(NoEmpty, 'On')
139  error(['"',filenev,'" has no element called "',varname,'", or it cannot be set.']);
140  end
141 end
142 
143 %% SaveLoadedVariables
144 %> @brief Save the loaded variables.
145 %> @param varargin Cell array of optional parameters (https://www.mathworks.com/help/matlab/ref/varargin.html):
146 %> @param 'filename' A string containing the path to the new file to be created instead of the original one.
147 function SaveLoadedVariables( obj, varargin )
148  p = inputParser;
149  p.addParameter('filename', obj.filename);
150  p.parse(varargin{:});
151 
152  filename = p.Results.filename;
153  LoadedVariable = obj.LoadedVariable;
154  save( filename, '-struct', 'LoadedVariable' );
155 end
156 
157 
158 %% ClearLoadedVariables
159 %> @brief Disposes the loaded file.
160 function ClearLoadedVariables( obj )
161  obj.LoadedVariable = [];
162 end
163 
164 
165 end %methods
166 
167 
168 end % Global end
function Transport(Energy, B)
Calculates the conductance at a given energy value.
function()
Structure param contains data structures describing the physical parameters of the scattering center ...
Definition: structures.m:45
A class providing interface to load variables from a file.
Definition: LoadFromFile.m:24