From 611f084a3dcf8a58de9728285ff6c061e2403bcd Mon Sep 17 00:00:00 2001 From: Simon Ward Date: Fri, 1 Dec 2017 13:24:42 +0100 Subject: [PATCH] Use the new preference class. (#54) * Created @swpref class * Added static properties * Added documentation to @swpref * Pref class (#53) * Created @swpref class * Created @swpref class (#51) * Added static properties * Added documentation to @swpref * cosmetic changes - updated help strings * More cosmetic changes * Fix recursion error --- dev/@spinw/corespec.m | 5 +- dev/@spinw/phonon.m | 3 +- dev/@spinw/scga.m | 4 +- dev/@spinw/scga3.m | 4 +- docs/docgenerator/docgen.m | 6 +- swfiles/+ndbase/pso.m | 3 +- swfiles/+swplot/activefigure.m | 3 +- swfiles/+swplot/arrow.m | 4 +- swfiles/+swplot/circle.m | 4 +- swfiles/+swplot/close.m | 7 +- swfiles/+swplot/cylinder.m | 4 +- swfiles/+swplot/ellipsoid.m | 3 +- swfiles/+swplot/figure.m | 3 +- swfiles/+swplot/legend.m | 3 +- swfiles/+swplot/plot.m | 7 +- swfiles/+swplot/plotatom.m | 8 +- swfiles/+swplot/plotbase.m | 5 +- swfiles/+swplot/plotbond.m | 5 +- swfiles/+swplot/plotcell.m | 5 +- swfiles/+swplot/plotchem.m | 5 +- swfiles/+swplot/plotion.m | 5 +- swfiles/+swplot/plotmag.m | 7 +- swfiles/+swplot/text.m | 3 +- swfiles/+swplot/tooltip.m | 4 +- swfiles/+swpref/getpref.m | 49 -- swfiles/+swpref/pref.m | 135 ------ swfiles/+swpref/setpref.m | 53 --- swfiles/@spinw/anneal.m | 3 +- swfiles/@spinw/annealloop.m | 3 +- swfiles/@spinw/disp.m | 3 +- swfiles/@spinw/fitspec.m | 4 +- swfiles/@spinw/fourier.m | 3 +- swfiles/@spinw/gencoupling.m | 4 +- swfiles/@spinw/getmatrix.m | 3 +- swfiles/@spinw/horace.m | 3 +- swfiles/@spinw/moment.m | 3 +- swfiles/@spinw/optmagsteep.m | 3 +- swfiles/@spinw/optmagstr.m | 3 +- swfiles/@spinw/plot.m | 4 +- swfiles/@spinw/powspec.m | 6 +- swfiles/@spinw/spinwave.m | 14 +- swfiles/@spinw/spinwavesym.m | 5 +- swfiles/@spinw/structfact.m | 3 +- swfiles/{+swpref => @swpref}/Contents.m | 4 +- swfiles/@swpref/export.m | 67 +++ swfiles/@swpref/import.m | 62 +++ swfiles/@swpref/private/datastruct.m | 141 ++++++ swfiles/@swpref/swpref.m | 567 ++++++++++++++++++++++++ swfiles/sw_import.m | 4 +- swfiles/sw_instrument.m | 3 +- swfiles/sw_model.m | 4 +- swfiles/sw_plotspec.m | 3 +- swfiles/sw_qgrid.m | 3 +- swfiles/sw_timeit.m | 3 +- swfiles/sw_tofres.m | 5 +- swfiles/swdoc.m | 4 +- test/sw_test.m | 3 +- tutorials/publish/publish_PSI.m | 13 +- tutorials/publish/tutorial30.m | 2 +- tutorials/publish/tutorial33.m | 4 +- tutorials/publish/tutorial35.m | 4 +- tutorials/publish/tutorial8.m | 2 +- tutorials/tutorial/YIG.m | 6 +- 63 files changed, 988 insertions(+), 330 deletions(-) delete mode 100644 swfiles/+swpref/getpref.m delete mode 100644 swfiles/+swpref/pref.m delete mode 100644 swfiles/+swpref/setpref.m rename swfiles/{+swpref => @swpref}/Contents.m (88%) create mode 100644 swfiles/@swpref/export.m create mode 100644 swfiles/@swpref/import.m create mode 100644 swfiles/@swpref/private/datastruct.m create mode 100644 swfiles/@swpref/swpref.m diff --git a/dev/@spinw/corespec.m b/dev/@spinw/corespec.m index 92e0b0b5c..ac9037755 100644 --- a/dev/@spinw/corespec.m +++ b/dev/@spinw/corespec.m @@ -124,12 +124,13 @@ % whether the structure is incommensurate incomm = any(abs(km-round(km)) > param.tol); -fid = swpref.getpref('fid',true); +pref = swpref; +fid = pref.fid; nHkl = size(hkl,2); % use mex file by default? -useMex = swpref.getpref('usemex',[]); +useMex = pref.usemex; % Create the interaction matrix and atomic positions in the extended % magnetic unit cell. diff --git a/dev/@spinw/phonon.m b/dev/@spinw/phonon.m index 3499c95e4..b95c94bf3 100644 --- a/dev/@spinw/phonon.m +++ b/dev/@spinw/phonon.m @@ -98,7 +98,8 @@ % solve the eigenvalue problem %[ea,om2] = eigenshuffle(Dab); -[ea,om2] = eigorth(Dab, param.omega_tol, swpref.getpref('usemex',[])); +pref = swpref; +[ea,om2] = eigorth(Dab, param.omega_tol, pref.usemex); om = sqrt(real(om2)); diff --git a/dev/@spinw/scga.m b/dev/@spinw/scga.m index b3bf585dc..12b78a035 100644 --- a/dev/@spinw/scga.m +++ b/dev/@spinw/scga.m @@ -61,8 +61,10 @@ spectra.datestart = datestr(now); end +pref = swpref; + if param.fid == -1 - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end diff --git a/dev/@spinw/scga3.m b/dev/@spinw/scga3.m index 9336c0fdc..0468a15eb 100644 --- a/dev/@spinw/scga3.m +++ b/dev/@spinw/scga3.m @@ -41,7 +41,9 @@ param.plot = false; end -fid = swpref.getpref('fid',true); +pref = swpref; +fid = pref.fid; + kBT = param.T*obj.unit.kB; beta = 1./kBT; diff --git a/docs/docgenerator/docgen.m b/docs/docgenerator/docgen.m index 3ad2fd02f..27616df39 100644 --- a/docs/docgenerator/docgen.m +++ b/docs/docgenerator/docgen.m @@ -1,13 +1,13 @@ %% setup help generator options -swPath = {'swfiles/@spinw' 'swfiles' 'swfiles/+swplot' 'swfiles/+swpref' 'swfiles/+swsym' 'swfiles/+swfunc' 'swfiles/+ndbase'}; +swPath = {'swfiles/@spinw' 'swfiles' 'swfiles/+swplot' 'swfiles/@swpref' 'swfiles/+swsym' 'swfiles/+swfunc' 'swfiles/+ndbase'}; swr = sw_rootdir; swPath = cellfun(@(C)[swr C],swPath,'UniformOutput',false); swver = sw_version; outPath = '~/spinwdoc_git'; docPath = '~/spinw_git/docs'; -upload = true; -recalc = true; +upload = false; +recalc = false; %% generate help diff --git a/swfiles/+ndbase/pso.m b/swfiles/+ndbase/pso.m index 73ef07abb..d881cf743 100644 --- a/swfiles/+ndbase/pso.m +++ b/swfiles/+ndbase/pso.m @@ -126,9 +126,10 @@ inpForm.size = [inpForm.size {[1 1] [1 1] [1 Np] [1 Np] [1 1] [1 1]}]; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; if param.tid == -1 - param.tid = swpref.getpref('tid',[]); + param.tid = pref.tid; end % parameter boundaries diff --git a/swfiles/+swplot/activefigure.m b/swfiles/+swplot/activefigure.m index 3cd2739d3..182c2961b 100644 --- a/swfiles/+swplot/activefigure.m +++ b/swfiles/+swplot/activefigure.m @@ -54,7 +54,8 @@ end % tag for active figure -activeTag = swpref.getpref('tag',[]); +pref = swpref; +activeTag = pref.tag; % tag for inactive figures inactiveTag = ['inactive_' activeTag]; diff --git a/swfiles/+swplot/arrow.m b/swfiles/+swplot/arrow.m index dfef24383..5b8904b6b 100644 --- a/swfiles/+swplot/arrow.m +++ b/swfiles/+swplot/arrow.m @@ -108,8 +108,10 @@ end +pref = swpref; + if isempty(nPatch) - nPatch = swpref.getpref('npatch',[]); + nPatch = pref.npatch; end if numel(rStart)==3 diff --git a/swfiles/+swplot/circle.m b/swfiles/+swplot/circle.m index 612c945af..38ba50c77 100644 --- a/swfiles/+swplot/circle.m +++ b/swfiles/+swplot/circle.m @@ -93,10 +93,12 @@ argExt = {varargin{4:end}}; end +pref = swpref; + if nArgExt > 0 N = argExt{1}; else - N = swpref.getpref('npatch',[]); + N = pref.npatch; end if numel(r0) == 3 diff --git a/swfiles/+swplot/close.m b/swfiles/+swplot/close.m index dfbe7538d..00f3dc77c 100644 --- a/swfiles/+swplot/close.m +++ b/swfiles/+swplot/close.m @@ -23,9 +23,12 @@ function close(varargin) % [swplot.figure] % +pref = swpref; + + if nargin == 0 % check if there is any swplot figure - activeTag = swpref.getpref('tag',[]); + activeTag = pref.tag; inactiveTag = ['inactive_' activeTag]; if isempty([findobj('tag',activeTag) findobj('tag',inactiveTag)]) % nothing to close @@ -45,7 +48,7 @@ function close(varargin) close(hFigure); else % close all swplot figure - activeTag = swpref.getpref('tag',[]); + activeTag = pref.tag; % tag for inactive figures inactiveTag = ['inactive_' activeTag]; diff --git a/swfiles/+swplot/cylinder.m b/swfiles/+swplot/cylinder.m index 8186eceea..18568003e 100644 --- a/swfiles/+swplot/cylinder.m +++ b/swfiles/+swplot/cylinder.m @@ -65,6 +65,8 @@ % [swplot.arrow] % +pref = swpref; + if nargin == 0 swhelp swplot.cylinder return @@ -102,7 +104,7 @@ end if isempty(nPatch) - nPatch = swpref.getpref('npatch',[]); + nPatch = pref.npatch; end if nArgExt > 1 diff --git a/swfiles/+swplot/ellipsoid.m b/swfiles/+swplot/ellipsoid.m index 4c56edd3e..c388a2176 100644 --- a/swfiles/+swplot/ellipsoid.m +++ b/swfiles/+swplot/ellipsoid.m @@ -53,6 +53,7 @@ % % [matlab.triangulation] \| [swplot.icomesh] % +pref = swpref; if nargin == 0 swhelp swplot.ellipsoid @@ -95,7 +96,7 @@ if isnumeric(mesh) % limit the largest mesh to plot to avoid slowing down Matlab too much - mesh = min(mesh,swpref.getpref('maxmesh',[])); + mesh = min(mesh,pref.maxmesh); % generate mesh mesh = swplot.icomesh(mesh); end diff --git a/swfiles/+swplot/figure.m b/swfiles/+swplot/figure.m index 54f7f8110..5d08a0e9e 100644 --- a/swfiles/+swplot/figure.m +++ b/swfiles/+swplot/figure.m @@ -35,6 +35,7 @@ % if ~isempty(get(0,'CurrentFigure')) % oldAxis = get(gcf,'CurrentAxes'); % end +pref = swpref; if nargin == 0 mode = 'hg'; @@ -76,7 +77,7 @@ 'Name', 'swplot',... 'DockControls', 'off',... 'PaperType', 'A4',... - 'Tag', swpref.getpref('tag',[]),... + 'Tag', pref.tag,... 'Toolbar', 'figure',... 'DeleteFcn', @closeSubFigs... ); diff --git a/swfiles/+swplot/legend.m b/swfiles/+swplot/legend.m index 912403fab..f04f93059 100644 --- a/swfiles/+swplot/legend.m +++ b/swfiles/+swplot/legend.m @@ -45,6 +45,7 @@ % : Handle of the swplot figure, default value is the handle of the active % figure. % +pref = swpref; if nargin == 0 switch0 = 'on'; @@ -123,7 +124,7 @@ lColor = lDat.color; % get the stored fontsize -fontSize = swpref.getpref('fontsize',[]); +fontSize = pref.fontsize; for ii = 1:numel(lType) switch lType(ii) diff --git a/swfiles/+swplot/plot.m b/swfiles/+swplot/plot.m index d60748005..fb87595b8 100644 --- a/swfiles/+swplot/plot.m +++ b/swfiles/+swplot/plot.m @@ -151,9 +151,10 @@ % [swplot.color] \| [swplot.add] % -P0 = swpref.getpref('npatch',[]); -M0 = swpref.getpref('nmesh',[]); -fontSize0 = swpref.getpref('fontsize',[]); +pref = swpref; +P0 = pref.npatch; +M0 = pref.nmesh; +fontSize0 = pref.fontsize; inpForm.fname = {'type' 'name' 'text' 'position' 'label' 'legend' 'color' 'unit' 'figure' 'lineStyle'}; inpForm.defval = {[] [] '' [] [] [] [] 'lu' [] '-' }; diff --git a/swfiles/+swplot/plotatom.m b/swfiles/+swplot/plotatom.m index f6f090cd5..302371b9c 100644 --- a/swfiles/+swplot/plotatom.m +++ b/swfiles/+swplot/plotatom.m @@ -133,10 +133,12 @@ % sObject = swplot.findobj(hFigure,'name','atom')`. % +pref = swpref; + % default values -fontSize0 = swpref.getpref('fontsize',[]); -nMesh0 = swpref.getpref('nmesh',[]); -nPatch0 = swpref.getpref('npatch',[]); +fontSize0 = pref.fontsize; +nMesh0 = pref.nmesh; +nPatch0 = pref.npatch; inpForm.fname = {'range' 'legend' 'label' 'dtext' 'fontsize' 'radius0'}; inpForm.defval = {[] true false 0.1 fontSize0 0.3 }; diff --git a/swfiles/+swplot/plotbase.m b/swfiles/+swplot/plotbase.m index dec65efff..61c7ab921 100644 --- a/swfiles/+swplot/plotbase.m +++ b/swfiles/+swplot/plotbase.m @@ -97,10 +97,11 @@ % % default values +pref = swpref; col0 = swplot.color({'red' 'green' 'blue'}); d0 = ones(1,3); -nMesh0 = swpref.getpref('nmesh',[]); -nPatch0 = swpref.getpref('npatch',[]); +nMesh0 = pref.nmesh; +nPatch0 = pref.npatch; inpForm.fname = {'range' 'mode' 'figure' 'color' 'R' 'alpha' 'lhead' 'shift'}; inpForm.defval = {[] 'abc' [] col0 0.06 30 0.5 [0;0;0]}; diff --git a/swfiles/+swplot/plotbond.m b/swfiles/+swplot/plotbond.m index 3e01c0eac..44a2bc754 100644 --- a/swfiles/+swplot/plotbond.m +++ b/swfiles/+swplot/plotbond.m @@ -222,8 +222,9 @@ % default values %fontSize0 = swpref.getpref('fontsize',[]); -nMesh0 = swpref.getpref('nmesh',[]); -nPatch0 = swpref.getpref('npatch',[]); +pref = swpref; +nMesh0 = pref.nmesh; +nPatch0 = pref.npatch; inpForm.fname = {'range' 'legend' 'label' 'zero' 'scale' 'radius0' 'mode2' 'linewidth'}; inpForm.defval = {[] true true true 1/3 0.05 [] 'fix' }; diff --git a/swfiles/+swplot/plotcell.m b/swfiles/+swplot/plotcell.m index 0ab729d04..e4cbf656a 100644 --- a/swfiles/+swplot/plotcell.m +++ b/swfiles/+swplot/plotcell.m @@ -119,8 +119,9 @@ % % default values -nMesh0 = swpref.getpref('nmesh',[]); -nPatch0 = swpref.getpref('npatch',[]); +pref = swpref; +nMesh0 = pref.nmesh; +nPatch0 = pref.npatch; inpForm.fname = {'range' 'mode' 'figure' 'color' 'linestyle' 'linewidth'}; inpForm.defval = {[] 'single' [] 'auto' '--' 1 }; diff --git a/swfiles/+swplot/plotchem.m b/swfiles/+swplot/plotchem.m index 9220845df..7bb9ef4cd 100644 --- a/swfiles/+swplot/plotchem.m +++ b/swfiles/+swplot/plotchem.m @@ -148,8 +148,9 @@ % % default values -nMesh0 = swpref.getpref('nmesh',[]); -nPatch0 = swpref.getpref('npatch',[]); +pref = swpref; +nMesh0 = pref.nmesh; +nPatch0 = pref.npatch; inpForm.fname = {'range' 'legend' 'label' 'unit' 'mode' 'atom1' 'atom2' 'copy'}; inpForm.defval = {[] true true 'lu' 'poly' 1 2 false }; diff --git a/swfiles/+swplot/plotion.m b/swfiles/+swplot/plotion.m index c6c9db340..a298263dc 100644 --- a/swfiles/+swplot/plotion.m +++ b/swfiles/+swplot/plotion.m @@ -128,8 +128,9 @@ % default values %fontSize0 = swpref.getpref('fontsize',[]); -nMesh0 = swpref.getpref('nmesh',[]); -nPatch0 = swpref.getpref('npatch',[]); +pref = swpref; +nMesh0 = pref.nmesh; +nPatch0 = pref.npatch; inpForm.fname = {'range' 'legend' 'label' 'scale' 'linewidth' 'alpha'}; inpForm.defval = {[] true true 1/3 0.5 0.3 }; diff --git a/swfiles/+swplot/plotmag.m b/swfiles/+swplot/plotmag.m index 1fb8650dc..cd3f24797 100644 --- a/swfiles/+swplot/plotmag.m +++ b/swfiles/+swplot/plotmag.m @@ -143,9 +143,10 @@ % default values -fontSize0 = swpref.getpref('fontsize',[]); -nMesh0 = swpref.getpref('nmesh',[]); -nPatch0 = swpref.getpref('npatch',[]); +pref = swpref; +fontSize0 = pref.fontsize; +nMesh0 = pref.nmesh; +nPatch0 = pref.npatch; inpForm.fname = {'range' 'legend' 'label' 'dtext' 'fontsize' 'radius0' }; inpForm.defval = {[] true true 0.1 fontSize0 0.06 }; diff --git a/swfiles/+swplot/text.m b/swfiles/+swplot/text.m index 99dadcb90..87bca0c26 100644 --- a/swfiles/+swplot/text.m +++ b/swfiles/+swplot/text.m @@ -44,6 +44,7 @@ end fontSize = []; +pref = swpref; if numel(varargin{1}) == 1 % first input figure handle @@ -73,7 +74,7 @@ end if isempty(fontSize) - fontSize = swpref.getpref('fontsize',[]); + fontSize = pref.fontsize; end hText = gobjects(1,nText); diff --git a/swfiles/+swplot/tooltip.m b/swfiles/+swplot/tooltip.m index caa4ae773..ba0f94820 100644 --- a/swfiles/+swplot/tooltip.m +++ b/swfiles/+swplot/tooltip.m @@ -59,8 +59,8 @@ if nargin == 0 text0 = 'on'; end - -fontSize = swpref.getpref('fontsize',[]); +pref = swpref; +fontSize = pref.fontsize; if nargin < 2 || isempty(hFigure) % find active figure diff --git a/swfiles/+swpref/getpref.m b/swfiles/+swpref/getpref.m deleted file mode 100644 index 74ae55c64..000000000 --- a/swfiles/+swpref/getpref.m +++ /dev/null @@ -1,49 +0,0 @@ -function rPref = getpref(prefName, varargin) -% returns SpinW global preferences -% -% ### Syntax -% -% `allPref = swpref.getpref` -% -% `selPref = swpref.getpref(prefName)` -% -% `val = swpref.getpref(prefName,true)` -% -% `rPref = swpref.getpref('default')` -% -% ### Description -% -% `allPref = swpref.getpref` returns all preference in a struct where each -% field-value pair corresponds to a prefernce name-value pair. -% -% `selPref = swpref.getpref(prefName)` returns a struct that contains the -% value, name and label of the selected preference. -% -% `val = swpref.getpref(prefName,true)` just returns the stored value -% corresponding to `prefName` preference. -% -% `rPref = swpref.getpref('default')` returns the default preference names, -% values and labels of each preferences. -% -% {{note The preferences are reset after every restart of Matlab, unlike the -% Matlab built-in preferences that are persistent between Matlab sessions. -% If you want certain preferences to keep after closing matlab, define them -% in the `startup.m` file.}} -% -% ### See Also -% -% [swpref.setpref] -% - -if nargin == 0 - % return all current values - rPref = swpref.pref([]); - % convert into single struct - rPref = [{rPref(:).name};{rPref(:).val}]; - rPref = struct(rPref{:}); -else - rPref = swpref.pref(prefName,'get',varargin{:}); -end - - -end \ No newline at end of file diff --git a/swfiles/+swpref/pref.m b/swfiles/+swpref/pref.m deleted file mode 100644 index feab3709b..000000000 --- a/swfiles/+swpref/pref.m +++ /dev/null @@ -1,135 +0,0 @@ -function rPref = pref(prefName, mode, value) -% internal helper for SpinW preferences -% -% ### Description -% -% {{warning Internal helper function for the SpinW preferences.}} -% -% ### See Also -% -% [swpref.setpref] \| [swpref.getpref] -% - -if nargin == 0 - return -end - -% the storage name within built-in getpref/setpref -%store = 'spinw_global'; - -prefName = lower(prefName); - -% default link to the online documentation -docurl0 = 'https://tsdev.github.io/spinwdoc'; - -% default values -dn = {'fid' 'expert' 'tag' 'nmesh' 'maxmesh' 'npatch' 'fontsize' 'tid' 'colormap' 'usemex' 'docurl'}; -dv = {1 0 'swplot' 1 6 20 12 1 @cm_inferno false docurl0 }; - -dl = {... - 'file identifier for text output, default value is 1 (Command Window)'... - 'expert mode (1) gives less warnings (not recommended), default value is 0'... - 'defines the tag property of the crystal structure plot figures'... - 'default number of subdivision of the icosahedron for plotting'... - 'maximum number of subdivision of the icosahedron for plotting'... - 'number of edges for patch object'... - 'fontsize for plotting'... - 'identifier how the timer is shown, default value is 1 (Command Window), value 2 gives graphical output'... - 'default colormap'... - 'if true, mex files are used in the spin wave calculation'... - 'url to the documentation server'... - }; - -dPref = struct('val',{},'name',{},'label',{}); - -[dPref(1:numel(dv),1).name] = dn{:}; -[dPref(:).label] = dl{:}; -[dPref(:).val] = dv{:}; - -% store the preferences in a persistent variable -persistent sPref -% lock the file in memory -mlock - -if isempty(sPref) - % start with the default values - sPref = dPref; -end - -if isempty(prefName) - % just return all current values - rPref = sPref; - return -end - -switch mode - case 'get' - - if ~isempty(prefName) - if strcmp(prefName,'default') - % return default preference values - rPref = dPref; - return - end - - % if a specific value is requested, check if it exist in the default value - % list - iPref = find(strcmp(prefName,{dPref(:).name}),1); - if isempty(iPref) - error('pref:WrongName','The requested SpinW preference does not exists!'); - end - - % if a specific value is requested and it exists, return it - rPref = sPref(iPref); - - if nargin>2 - rPref = rPref.val; - end - - return - else - % return all stored values - rPref = dPref; - % overwrite default values for existing preferences - if ~isempty(sPref) - fPref = fieldnames(sPref); - for ii = 1:numel(fPref) - rPref(strcmp(fPref{ii},{dPref(:).name})).val = sPref.(fPref{ii}); - end - end - - end - case 'set' - % set preferences - if strcmp(prefName,'default') - sPref = dPref; - return - end - - % check if the preference name exists - iPref = find(strcmp(prefName,{dPref(:).name}),1,'first'); - if ~isempty(iPref) - % check if the preferences label contains a choice string - %str0 = strsplit(dPref(iPref).label,' '); - str0 = regexp(dPref(iPref).label,' ','split'); - %opt0 = strsplit(str0{end},'/'); - opt0 = regexp(str0{end},'/','split'); - - if numel(opt0) > 1 - % there is a choice of different string options - if ~ischar(value) || ~any(strcmp(value,opt0)) - error('pref:WrongInput',['The selected preference has a restricted choice: ' str0{end} '!']) - end - sPref(iPref).val = value; - else - % the value has to be a scalar - % TODO check for other type of values - sPref(iPref).val = value; - end - - else - error('pref:WrongName','The given name is not a valid SpinW global preferences!'); - end -end - -end \ No newline at end of file diff --git a/swfiles/+swpref/setpref.m b/swfiles/+swpref/setpref.m deleted file mode 100644 index b223deb05..000000000 --- a/swfiles/+swpref/setpref.m +++ /dev/null @@ -1,53 +0,0 @@ -function setpref(prefName, varargin) -% sets SpinW global preferences -% -% ### Syntax -% -% `swpref.setpref(prefName, value)` -% -% `swpref.setpref('default')` -% -% ### Description -% -% `swpref.setpref(prefName, value)` sets the value of `prefName` -% preferences. -% -% `swpref.setpref('default')` resets all preference values to the default one. -% -% {{note The preferences are reset after every restart of Matlab, unlike the -% Matlab built-in preferences that are persistent between Matlab sessions. -% If you want certain preferences to keep after closing matlab, define them -% in the `startup.m` file.}} -% -% ### See Also -% -% [swpref.getpref] -% - -if nargin>0 && mod(nargin,2)==0 - % check for usemex option - mexopt = find(strcmpi('usemex',varargin)); - - if ~isempty(mexopt) && varargin{mexopt+1} - % check for the existence of the necessary mex files - if exist('chol_omp','file')==3 && exist('eig_omp','file')==3 - else - warning('setpref:MissingMex','Necessary mex files are missing, compile them!') - varargin{mexopt+1} = false; - end - end -end - -if nargin<=2 - swpref.pref(prefName,'set',varargin{:}); -elseif mod(nargin,2)==0 - arg = [{prefName} varargin]; - % multiple variable - for ii = 1:2:numel(arg) - swpref.pref(arg{ii},'set',arg{ii+1}); - end -else - error('setpref:WrongNumberOfArgument','Wrong number of input arguments!') -end - -end \ No newline at end of file diff --git a/swfiles/@spinw/anneal.m b/swfiles/@spinw/anneal.m index 482c40b9a..784d586e3 100644 --- a/swfiles/@spinw/anneal.m +++ b/swfiles/@spinw/anneal.m @@ -248,7 +248,8 @@ end % Text output file -fid = swpref.getpref('fid',true); +pref = swpref; +fid = pref.fid; % Creates random spin directions if param.random is true. mag_param = struct; diff --git a/swfiles/@spinw/annealloop.m b/swfiles/@spinw/annealloop.m index d1a73d505..ff120bee4 100644 --- a/swfiles/@spinw/annealloop.m +++ b/swfiles/@spinw/annealloop.m @@ -97,9 +97,10 @@ inpForm.soft = [inpForm.soft {false false false false}]; param = sw_readparam(inpForm,varargin{:}); +pref = swpref; if param.tid == -1 - param.tid = swpref.getpref('tid',[]); + param.tid = pref.tid; end % check output diff --git a/swfiles/@spinw/disp.m b/swfiles/@spinw/disp.m index 72ee2eeb9..a42f237ad 100644 --- a/swfiles/@spinw/disp.m +++ b/swfiles/@spinw/disp.m @@ -39,12 +39,13 @@ % Datastruct = datastruct; +pref = swpref; choiceStr = {'off' 'on'}; symbStr = choiceStr{obj.symbolic+1}; symmStr = choiceStr{obj.symmetry+1}; -fid = swpref.getpref('fid',true); +fid = pref.fid; if fid == 0 fidStr = 'none'; else diff --git a/swfiles/@spinw/fitspec.m b/swfiles/@spinw/fitspec.m index 97d4e4a18..ddf37181e 100644 --- a/swfiles/@spinw/fitspec.m +++ b/swfiles/@spinw/fitspec.m @@ -160,8 +160,8 @@ % % [spinw.spinwave] \| [spinw.matparser] \| [sw_egrid] \| [sw_neutron] \| [sw_readspec] % - -tid0 = swpref.getpref('tid',[]); +pref = swpref; +tid0 = pref.tid; inpForm.fname = {'epsilon' 'datapath' 'xmin' 'xmax' 'x0' 'func' 'plot'}; inpForm.defval = {1e-5 ' ' [] [] [] [] true }; diff --git a/swfiles/@spinw/fourier.m b/swfiles/@spinw/fourier.m index c7ac837dd..dd2a12fc8 100644 --- a/swfiles/@spinw/fourier.m +++ b/swfiles/@spinw/fourier.m @@ -100,6 +100,7 @@ inpForm.soft = {false false false true }; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; switch param.isomode case 'auto' @@ -112,7 +113,7 @@ if param.fid == -1 % Print output into the following file - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/@spinw/gencoupling.m b/swfiles/@spinw/gencoupling.m index b1302c4ad..26517a0d3 100644 --- a/swfiles/@spinw/gencoupling.m +++ b/swfiles/@spinw/gencoupling.m @@ -91,6 +91,8 @@ function gencoupling(obj, varargin) param = sw_readparam(inpForm, varargin{:}); +pref = swpref; + tol = param.tol; tolD = param.tolDist; @@ -103,7 +105,7 @@ function gencoupling(obj, varargin) end if param.fid == -1 - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/@spinw/getmatrix.m b/swfiles/@spinw/getmatrix.m index a693c6994..2c013b1b9 100644 --- a/swfiles/@spinw/getmatrix.m +++ b/swfiles/@spinw/getmatrix.m @@ -116,6 +116,7 @@ inpForm.soft = {false false false false true false false false}; param0 = sw_readparam(inpForm, varargin{:}); +pref = swpref; param = param0; tol = param.tol; @@ -126,7 +127,7 @@ end if param.fid == -1 - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/@spinw/horace.m b/swfiles/@spinw/horace.m index 32635cbc5..7460d1efe 100644 --- a/swfiles/@spinw/horace.m +++ b/swfiles/@spinw/horace.m @@ -129,6 +129,7 @@ warnState = warning('off','sw_readparam:UnreadInput'); param = sw_readparam(inpForm, varargin{:}); +pref = swpref; if ~isempty(param.param) % change matrix values for Horace data fitting @@ -147,7 +148,7 @@ end if param.fid == -1 - fid = swpref.getpref('fid',[]); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/@spinw/moment.m b/swfiles/@spinw/moment.m index 225b5f6d8..008b73cad 100644 --- a/swfiles/@spinw/moment.m +++ b/swfiles/@spinw/moment.m @@ -100,6 +100,7 @@ inpForm.size = {[1 1] [1 1] [1 1] [1 1] [1 1] [1 1]}; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; magstr = obj.magstr; @@ -111,7 +112,7 @@ incomm = any(abs(km-round(km)) > param.tol); if param.fid == -1 - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/@spinw/optmagsteep.m b/swfiles/@spinw/optmagsteep.m index 2a208d1dc..f6b0a623d 100644 --- a/swfiles/@spinw/optmagsteep.m +++ b/swfiles/@spinw/optmagsteep.m @@ -134,6 +134,7 @@ param = sw_readparam(inpForm,varargin{:}); +pref = swpref; if prod(param.nExt) == 0 error('spinw:optmagsteep:WrongInput','''nExt'' has to be larger than 0!'); @@ -141,7 +142,7 @@ % Text output file if param.fid == -1 - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/@spinw/optmagstr.m b/swfiles/@spinw/optmagstr.m index dd98ec4fe..7547ca91f 100644 --- a/swfiles/@spinw/optmagstr.m +++ b/swfiles/@spinw/optmagstr.m @@ -174,6 +174,7 @@ % creat initial magnetic structure warnState = warning('off','sw_readparam:UnreadInput'); param = sw_readparam(inpForm, varargin{:}); +pref = swpref; obj.genmagstr(param); @@ -189,7 +190,7 @@ nMagExt = length(S); if param.tid == -1 - param.tid = swpref.getpref('tid',[]); + param.tid = pref.tid; end diff --git a/swfiles/@spinw/plot.m b/swfiles/@spinw/plot.m index 96df077ff..ea5190716 100644 --- a/swfiles/@spinw/plot.m +++ b/swfiles/@spinw/plot.m @@ -102,8 +102,8 @@ % [swplot.plotatom] \| [swplot.plotmag] \| [swplot.plotion] \| % [swplot.plotbond] \| [swplot.plotbase] \| [swplot.plotcell] % - -fid = swpref.getpref('fid',true); +pref = swpref; +fid = pref.fid; % preparation fprintf0(fid,'Creating 3D plot... \n'); diff --git a/swfiles/@spinw/powspec.m b/swfiles/@spinw/powspec.m index ceba4dde8..6f6e9fdb4 100644 --- a/swfiles/@spinw/powspec.m +++ b/swfiles/@spinw/powspec.m @@ -193,11 +193,13 @@ return end +pref = swpref; + hklA = hklA(:)'; T0 = obj.single_ion.T; title0 = 'Powder LSWT spectrum'; -tid0 = swpref.getpref('tid',[]); +tid0 = pref.tid; inpForm.fname = {'nRand' 'Evect' 'T' 'formfact' 'formfactfun' 'tid' 'nInt'}; inpForm.defval = {100 zeros(1,0) T0 false @sw_mff tid0 1e3 }; @@ -218,7 +220,7 @@ param = sw_readparam(inpForm, varargin{:}); if param.fid == -1 - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/@spinw/spinwave.m b/swfiles/@spinw/spinwave.m index f9e559b21..d3b30094a 100644 --- a/swfiles/@spinw/spinwave.m +++ b/swfiles/@spinw/spinwave.m @@ -34,9 +34,9 @@ % model using `sw_model`. % % ``` -% >>tri = sw_model('triAF',1); -% >>spec = tri.spinwave({[0 0 0] [1 1 0]}); -% >>sw_plotspec(spec); +% >>tri = sw_model('triAF',1) +% >>spec = tri.spinwave({[0 0 0] [1 1 0]}) +% >>sw_plotspec(spec) % >>snapnow % ``` % @@ -232,6 +232,8 @@ % [spinw] \| [spinw.spinwavesym] \| [sw_mex] \| [spinw.powspec] \| [sortmode] % +pref = swpref; + % for linear scans create the Q line(s) if nargin > 1 hkl = sw_qscan(hkl); @@ -246,7 +248,7 @@ singWarn0 = warning('off','MATLAB:nearlySingularMatrix'); % use mex file by default? -useMex = swpref.getpref('usemex',[]); +useMex = pref.usemex; % calculate symbolic spectrum if obj is in symbolic mode if obj.symbolic @@ -308,11 +310,11 @@ end if param.tid == -1 - param.tid = swpref.getpref('tid',[]); + param.tid = pref.tid; end if param.fid == -1 - param.fid = swpref.getpref('fid',[]); + param.fid = pref.fid; end fid = param.fid; diff --git a/swfiles/@spinw/spinwavesym.m b/swfiles/@spinw/spinwavesym.m index d333d9ece..be132d51e 100644 --- a/swfiles/@spinw/spinwavesym.m +++ b/swfiles/@spinw/spinwavesym.m @@ -130,6 +130,7 @@ inpForm.size = {[1 1] [3 1] [1 1] [1 1] [1 1] [1 -1] [1 1]}; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; if param.norm param.vect = true; @@ -149,9 +150,9 @@ hkl = obj.unit.qmat*param.hkl; if param.fid == -1 - fid = swpref.getpref('fid',true); + fid = pref.fid; else - fid = param..fid; + fid = param.fid; end % Create the interaction matrix and atomic positions in the extended diff --git a/swfiles/@spinw/structfact.m b/swfiles/@spinw/structfact.m index 2220d7400..cf0ef6de6 100644 --- a/swfiles/@spinw/structfact.m +++ b/swfiles/@spinw/structfact.m @@ -144,9 +144,10 @@ inpF.soft = [inpF.soft {true false true false false false}]; param = sw_readparam(inpF, varargin{:}); +pref = swpref; if param.fid == -1 - fid = swpref.getpref('fid',[]); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/+swpref/Contents.m b/swfiles/@swpref/Contents.m similarity index 88% rename from swfiles/+swpref/Contents.m rename to swfiles/@swpref/Contents.m index c80147ab9..ac33f8d89 100644 --- a/swfiles/+swpref/Contents.m +++ b/swfiles/@swpref/Contents.m @@ -6,5 +6,5 @@ % % ### Files % -% swpref.getpref -% swpref.setpref +% import +% export \ No newline at end of file diff --git a/swfiles/@swpref/export.m b/swfiles/@swpref/export.m new file mode 100644 index 000000000..113d9de21 --- /dev/null +++ b/swfiles/@swpref/export.m @@ -0,0 +1,67 @@ +function success = export(obj,location) +% saves swpref object into a file +% +% ### Syntax +% +% `success = export(obj,location)` +% +% `success = export(obj)` +% +% ### Description +% +% `success = export(obj,location)` writes the preferences given in `obj` to +% a file location given by `location`. The file is in a basic `.json` +% format. +% +% `success = export(obj)` writes the preferences given in `obj` to +% the users home folder as `swprefs.json`. The file is in a basic `.json` +% format. +% +% ### See Also +% +% [swpref.import] +% + +props = obj.props; + +if nargin == 1 + location = [userpath filesep 'swprefs.json']; +end + + +f = fopen(location,'w'); +if f < 0 + % error + error('swpref:export:FileBlocked','Cannot write to file %s.',location); +end +fprintf(f, '{\n'); +for i = 1:length(props) + name = props(i).Name; + value = obj.(name); + if ~isstruct(value) + if isnumeric(value) + if i == length(props) + fprintf(f, '\t''%s'': %f\n',name,value); + else + fprintf(f, '\t''%s'': %f,\n',name,value); + end + elseif ischar(value) + if i == length(props) + fprintf(f, '\t''%s'': ''%s''\n',name,value); + else + fprintf(f, '\t''%s'': ''%s'',\n',name,value); + end + elseif isa(value,'function_handle') + if i == length(props) + fprintf(f, '\t''%s'': ''@%s''\n',name,func2str(value)); + else + fprintf(f, '\t''%s'': ''@%s'',\n',name,func2str(value)); + end + end + else + end +end +fprintf(f, '}\n'); +success = fclose(f); + +end \ No newline at end of file diff --git a/swfiles/@swpref/import.m b/swfiles/@swpref/import.m new file mode 100644 index 000000000..123a66059 --- /dev/null +++ b/swfiles/@swpref/import.m @@ -0,0 +1,62 @@ +function import(obj,location) +% imports swpref object from file +% +% ### Syntax +% +% +% `obj = import(obj)` +% +% `obj = import(obj,location)` +% +% ### Description +% +% `obj = import(obj)` loads the preferences given in by the file +% `swprefs.json` in the users home folder. It sets the preferences and +% returns a new preference object. +% +% `obj = import(obj,location)` loads the preferences given in by the file +% specified by `location`, sets the preferences and returns a new +% preference object. +% +% ### See Also +% +% [swpref.export] +% + +if nargin == 1 + location = [userpath filesep 'swprefs.json']; +end + +if ~exist(location,'file') + error('spref:ReadError','Can not find the preference file\n%s',location) +end + +f = fopen(location,'r'); +c = onCleanup(@() feval(@fclose,f)); +[~] = fgetl(f); + +while ~feof(f) + line = fgetl(f); + if strcmp(line,'}') + break + end + name_val = textscan(line,'%s'); + name = name_val{1}{1}(2:end-2); + value = name_val{1}{2}; + if strcmp(value(end),',') + value = value(1:end-1); + end + if strcmp(value(end),'''') + % We have a str or fn + value = value(2:end-1); % Strip off the ' + if strcmp(value(1),'@') + value = str2func(value(2:end)); + end + else + % We have a numeric + value = str2double(value); + end + obj.(name) = value; +end + +end \ No newline at end of file diff --git a/swfiles/@swpref/private/datastruct.m b/swfiles/@swpref/private/datastruct.m new file mode 100644 index 000000000..1a167228e --- /dev/null +++ b/swfiles/@swpref/private/datastruct.m @@ -0,0 +1,141 @@ +function d = datastruct() +% Function called to create a default preference object. +% +% {{warning Internal function for the Spin preferences.}} +% +% ### Syntax +% +% 'prefs = datastruct()' +% +% ### Description +% +% 'prefs = datastruct()' creates a structure with the following fields: +% +% 'Name' a cell array giving the name of each dynamic property. +% +% 'Validation' a cell array with functions to evaluate when a +% property is set. +% +% 'Value' a cell array giving the default value of a dynamic property +% +% 'Label' a cell array giving a short description on the dynamic +% property. +% + +d.Name = { + 'fid',... + 'expert',... + 'tag',... + 'nmesh',... + 'maxmesh',... + 'npatch',... + 'fontsize',... + 'tid',... + 'colormap',... + 'usemex',... + 'docurl' + }; + +Size = { + [1, 1],... + [1, 1],... + [ ],... + [1, 1],... + [1, 1],... + [1, 1],... + [1, 1],... + [1, 1],... + [1, 1],... + [1, 1],... + [ ] + }; + +d.Validation = { + {@isnumeric, @mustBeInteger, @mustBeNonnegative, @(x) check_size(x,Size{1}), @(x) mustBeLessThan(x,256)},... + {@islogical, @(x) check_size(x,Size{2})},... + {@ischar},... + {@isnumeric, @mustBeInteger, @mustBeNonnegative, @(x) check_size(x,Size{4}), @(x) mustBeGreaterThanOrEqual(x,1), @(x) mustBeLessThan(x,256)},... + {@isnumeric, @mustBeInteger, @mustBeNonnegative, @(x) check_size(x,Size{5}), @(x) mustBeGreaterThan(x,1), @(x) mustBeLessThan(x,256)},... + {@isnumeric, @mustBeInteger, @mustBeNonnegative, @(x) check_size(x,Size{6}), @(x) mustBeGreaterThan(x,1), @(x) mustBeLessThan(x,256)},... + {@isnumeric, @mustBeInteger, @mustBeNonnegative, @(x) check_size(x,Size{7}), @(x) mustBeGreaterThan(x,4), @(x) mustBeLessThan(x,256)},... + {@isnumeric, @mustBeInteger, @mustBeNonnegative, @(x) check_size(x,Size{8}), @(x) mustBeLessThan(x,256)},... + {@(x) check_size(x,Size{9}), @check_mex, @(x) isa(x,'function_handle')},... + {@(x) check_size(x,Size{10}), @islogical},... + {@ischar, @(x) strfind(x,'http')} + }; + +d.Value = { + 1,... + false,... + 'swplot',... + 1,... + 6,... + 20,... + 12,... + 1,... + @cm_inferno,... + false,... + 'https://tsdev.github.io/spinwdoc' + }; + +d.Label = { + 'file identifier for text output, default value is 1 (Command Window)'... + 'expert mode (1) gives less warnings (not recommended), default value is 0'... + 'defines the tag property of the crystal structure plot figures'... + 'default number of subdivision of the icosahedron for plotting'... + 'maximum number of subdivision of the icosahedron for plotting'... + 'number of edges for patch object'... + 'fontsize for plotting'... + 'identifier how the timer is shown, default value is 1 (Command Window), value 2 gives graphical output'... + 'default colormap'... + 'if true, mex files are used in the spin wave calculation'... + 'url to the documentation server'... + }; + + function out = check_size(obj,S) + % checks to see if an object is the wrong size. + % + % {{warning Internal function for the Spin preferences.}} + % + % ### Syntax + % + % 'logical = check_size(toBeChecked,size)' + % + % ### Description + % + % 'logical = check_size(toBeChecked,size)' checks to see if an + % object 'obj 'is the expected size given by 'size'. An error is + % thrown if there is a difference. + % + + sz = size(obj); + if ~all(sz == S) + error('spref:WrongSize','Value to be asigned is the wrong size [%i, %i] not [%i, %i]',sz(1), sz(2), S(1), S(2)) + else + out = 1; + end + end + + function out = check_mex(~) + % checks to see if mex files are available. + % + % {{warning Internal function for the Spin preferences.}} + % + % ### Syntax + % + % 'logical = check_mex(obj)' + % + % ### Description + % + % 'logical = check_mex(obj)' checks to see if files 'chol_omp' and + % 'eig_omp' are present in the MATLAB path.An error is thrown if + % they do not exist. + % + + if ~(exist('chol_omp','file')==3 && exist('eig_omp','file')==3) + error('spref:MissingMex','Necessary mex files are missing, compile them!') + else + out = 1; + end + end +end \ No newline at end of file diff --git a/swfiles/@swpref/swpref.m b/swfiles/@swpref/swpref.m new file mode 100644 index 000000000..f7f47c6c9 --- /dev/null +++ b/swfiles/@swpref/swpref.m @@ -0,0 +1,567 @@ +classdef swpref < dynamicprops + % class to store and retrieve persistent settings + % + % ### Syntax + % + % `pref = swpref` + % + % `pref = swpref('default')` + % + % ### Description + % + % `pref = swpref` retrieves and creates a preference object. + % + % `pref = swpref('default')` resets all preferences to default values. + % + % The settings sotred in the `swpref` class for spinw objects will + % persist during a single Matlab session. It is different from the + % Matlab built-in preferences, as swpref resets all settings to factory + % default after every restart of Matlab. + % + % ### Examples + % + % We change the fontsize value and show that it is retained even when a + % new instance of the object is created: + % + % ``` + % >>pref = swpref + % >>pref.fontsize>> + % >>pref.fontsize = 18 + % >>pref2 = swpref + % >>pref.fontsize>> + % >>pref2.fontsize>> + % ``` + % + % ### Properties + % + % Properties can be changed by directly assigning a new value to them. + % Once a new value to a given property is assigned, it will be retained + % until the end of a MATLAB session, even if a new class instance is + % created. + % + % ### Methods + % + % Methods are the different commands that require an `swpref` object as + % a first input, thus they can be called as `method1(obj,...)`, + % alternatively the equivalent command is `obj.method1(...)`. + % + % swpref.get + % swpref.set + % swpref.export + % swpref.import + % + % ### Commands + % + % Commands are methods which can be called without first creating a + % preference object 'swpref.command(....)'. + % + % swpref.getpref + % swpref.setpref + % + + properties(Hidden = true, Access=private) + % stores the details to create and check dynamic properties. + % + % `Name` a cell array giving the name of each dynamic property. + % + % `Validation` a cell array with functions to evaluate when a + % property is set. + % + % `Value` a cell array giving the default value of a dynamic property + % + % `Label` a cell array giving a short description on the dynamic + % property. + % + % These details are retrieved from the private file `datastruct.m`. + % + Name + Validation + Value + Label + end + + properties (Hidden=true, Access = private) + % stores the details of the dynamic properties. + props = meta.DynamicProperty.empty + end + + methods + function obj = swpref(opt) + % Spin preference constructor. + % + % ### Syntax + % + % `pref = swpref` + % + % `pref = swpref('default')` + % + % + % ### Description + % + % `pref = swpref` retrieves and creates a preference object. + % + % `pref = swpref('default')` resets all preferences to default values. + % + % + % {{note The preferences are reset after every restart of Matlab, unlike the + % Matlab built-in preferences that are persistent between Matlab sessions. + % If you want certain preferences to keep after closing matlab, use the + % 'pref.export(fileLocation)' and 'pref.import(fileLocation)' functions. + % These can be added to your startup file.}} + % + % ### See Also + % + % [swpref.get], [swpref.set] + % + + if nargin > 0 + if ischar(opt) && strcmpi(opt,'default') + opt = 0; + else + opt = 1; + end + else + opt = 1; + end + + data = datastruct; + sPref = obj.get_set_static(); + + obj.Name = data.Name; + obj.Validation = data.Validation; + obj.Value = data.Value; + obj.Label = data.Label; + + if opt && ~isempty(sPref) + f = fieldnames(sPref); + for i = 1:length(f) + ind = strcmp(f{i},obj.Name); + if any(ind) + obj.Value{ind} = sPref.(f{i}); + end + end + else + f = obj.Name; + for i = 1:length(f) + obj.get_set_static(f{i},obj.Value{i}); + sPref.(f{i}) = obj.Value{i}; + end + end + + for i = 1:length(data.Name) + obj.props(i) = addprop(obj,obj.Name{i}); + obj.props(i).SetMethod = @(obj, val) set_data(obj,obj.Name{i}, val); + obj.props(i).GetMethod = @(obj) get_data(obj,obj.Name{i}); + if isfield(sPref,obj.Name{i}) + obj.(obj.Name{i}) = sPref.(obj.Name{i}); + else + obj.(obj.Name{i}) = obj.Value{i}; + end + end + end + + function varargout = get(obj,names) + % retrieves a preference value + % + % ### Syntax + % + % `value = get(obj, name)` + % + % `value = obj.get(name)` + % + % ### Description + % + % `value = get(obj, name)` gets the preference `name`. + % + % `value = obj.get(name)` gets the preference `name`. + % + % ### See Also + % + % [swpref.set] + % + + if nargin == 1 + error('swpref:GetError','You need to supply a parameter to get!'); + end + + if iscell(names) + j = 1; + for i = 1:legth(names) + if obj.check_names(names{i}) + varargout{j} = obj.(names{i}); %#ok + j = j+1; + else + error('swpref:GetError','There is no field %s in swpref',names{i}); + end + end + else + if obj.check_names(names) + varargout{1} = obj.(names); + else + error('swpref:GetError','There is no field %s in swpref',names); + end + end + end + + + function set(obj,names,values) + % sets a preference value + % + % ### Syntax + % + % `set(obj, name, value)` + % + % `obj.set(name, value)` + % + % ### Description + % + % `set(obj, name, value)` sets the preference `name` to the + % value given by `value` + % + % `obj.set(name, value)` sets the preference `name` to the + % value given by `value` + % + % ### See Also + % + % [swpref.get] + % + + if nargin < 2 + error('swpref:SetError','You need to supply a parameter,value pair to set!'); + end + + if iscell(names) && iscell(values) + if length(names) ~= length(values) + error('swpref:SetInputError','Names and Values must have the same length') + end + for i = 1:length(names) + if obj.check_names(names{i}) + obj.(names{i}) = values{i}; + else + error('swpref:SetError','There is no field %s in swpref',names{i}); + end + end + else + if obj.check_names(names) + obj.(names) = values; + else + error('swpref:SetError','There is no field %s in swpref',names); + end + end + end + + function disp(obj) + % function called when a preference is displayed. + % + % ### Syntax + % + % `disp(obj)` + % + % `obj.disp` + % + % ### Description + % + % `disp(obj)` shows a table of all dynamic properties including + % name, value and description. If a table is not supported only + % `name: value` is displayed for all properties. + % + + if verLessThan('MATLAB','8.2') + isTable = false; + else + isTable = true; + end + + this_Name = obj.Name'; + this_Label = obj.Label'; + this_Value = cell(size(this_Label)); + for i = 1:length(this_Name) + this_Value{i} = obj.(this_Name{i}); + end + + varName = {'Name', 'Value', 'Label'}; + var = {this_Name, this_Value, this_Label}; + + fprintf(sw_markdown(sprintf(... + [' `Swpref` object, [swpref] class:\n' ... + ' `Stored preferences:`\n']))) + if isTable + disp(table(var{:},'VariableNames',varName)) + else + disp(swpref.getpref); + end + end + end + + methods (Hidden=true, Access = private) + + function set_data(obj, name, val) + % Function called when a vairable is set. + % + % {{warning Internal function for the Spin preferences.}} + % + % ### Syntax + % + % 'set_data(obj, name, value)' + % + % ### Description + % + % 'set_data(obj, name, value)' sets the 'value' of parameter + % 'name' which is stored in persistent storage. + % + % ### See Also + % + % [swpref.setpref], [swpref.get_data] + % + + if ~obj.check_names(name) + error('swpref:SetError','There is no field %s in swpref',name); + end + + idx = strcmp(name,obj.Name); + if ~isempty(obj.Validation{idx}) + checks = obj.Validation{idx}; + for i = 1:length(checks) + feval(checks{i},val); + end + end + obj.get_set_static(name, val); + end + + function val = get_data(obj, name) + % Function called when a vairable is retrieved. + % + % {{warning Internal function for the Spin preferences.}} + % + % ### Syntax + % + % 'value = get_data(obj, name)' + % + % ### Description + % + % 'value = get_data(obj, name)' returns the value of parameter + % 'name' from persistent storage. + % + % ### See Also + % + % [swpref.setpref], [swpref.set_data] + % + + if obj.check_names(name) + val = obj.get_set_static(name); + else + error('swpref:GetError','There is no field %s in swpref',name); + end + end + + function valid = check_names(obj,name) + % Checking to see if a get/set name is valid. + % + % {{warning Internal function for the Spin preferences.}} + % + % ### Syntax + % + % 'logical = obj.check_names(name)' + % + % ### Description + % + % 'logical = obj.check_names(name)' returns true if 'name' is a + % valid field of 'obj' and false otherwise. + % + + valid = any(strcmp(name,fieldnames(obj))); + end + end + + methods (Static, Hidden = true) + function varargout = get_set_static(name,value) + % The internal session persistent storage of vairables. + % + % {{warning Internal function for the Spin preferences.}} + % + % ### Syntax + % + % 'values = get_set_static()' + % + % 'value' = get_set_static(prefName)' + % + % 'get_set_static(prefName, value)' + % + % ### Description + % + % 'values = get_set_static()' retrieves all the preferences in + % the storage. + % + % 'value' = get_set_static(prefName)' returns the value of + % preference given by 'prefName'. + % + % 'get_set_static(prefName, value)' sets the preference given + % by 'prefName' to value 'value' + % + % ### See Also + % + % [swpref.check_names], [swpref.get_data], [swpref.set_data] + % + + persistent sPref + if nargin == 0 + varargout{1} = sPref; + return + elseif nargin == 1 + varargout{1} = sPref.(name); + else + sPref.(name) = value; + end + end + end + + methods (Static) + function out = getpref(name,varargin) + % returns SpinW global preferences + % + % ### Syntax + % + % `allPref = swpref.getpref` + % + % `selPref = swpref.getpref(prefName)` + % + % `val = swpref.getpref(prefName,[])` + % + % ### Description + % + % `allPref = swpref.getpref` returns all preference in a struct where each + % field-value pair corresponds to a prefernce name-value pair. + % + % `selPref = swpref.getpref(prefName)` returns a struct that contains the + % value, name and label of the selected preference. + % + % `val = swpref.getpref(prefName,[])` just returns the stored value + % corresponding to `prefName` preference. + % + % {{note The preferences are reset after every restart of Matlab, unlike the + % Matlab built-in preferences that are persistent between Matlab sessions. + % If you want certain preferences to keep after closing matlab, define them + % in the `startup.m` file.}} + % + % {{warning This is a legacy function. It is better to use `pref = swpref` and then + % use regular `pref.prefName = value` or `set(pref, prefName, value)` syntax.}} + % + % ### See Also + % + % [swpref.setpref] + % + + obj = swpref; + if nargin == 0 + val = cellfun(@(C)obj.(C),obj.Name,'UniformOutput',false); + out = [obj.Name; val]; + out = struct(out{:}); + return + end + if ~obj.check_names(name) + error('swpref:GetError','There is no field %s in swpref',name); + end + if nargin > 1 + out = obj.get(name); + else + out.name = name; + out.label = obj.Label{strcmp(name,obj.Name)}; + out.val = obj.get(name); + end + end + + function setpref(name,val) + % sets SpinW global preferences + % + % ### Syntax + % + % `swpref.setpref(prefName, value)` + % + % ### Description + % + % `swpref.setpref(prefName, value)` sets the value of `prefName` + % preferences. + % + % {{note The preferences are reset after every restart of Matlab, unlike the + % Matlab built-in preferences that are persistent between Matlab sessions. + % If you want certain preferences to keep after closing matlab, define them + % in the `startup.m` file.}} + % + % {{warning This is a legacy function. It is better to use 'pref = swpref' and then + % use regular 'value = pref.prefName' or 'get(pref, prefName)' syntax.}} + % + % ### See Also + % + % [swpref.getpref] + % + + if nargin < 2 + error('swpref:SetInputError','There needs to be a valid name, value pair.'); + end + obj = swpref; + if ~obj.check_names(name) + error('swpref:SetError','There is no field %s in swpref',name); + end + obj.(name) = val; + end + + function export_prefs(varargin) + % saves swpref object into a file + % + % ### Syntax + % + % `success = swpref.export_prefs(location)` + % + % `success = swpref.export_prefs` + % + % ### Description + % + % `success = swpref.export_prefs(location)` writes the preferences given by swpref to + % a file location given by `location`. The file is in a basic `.json` + % format. + % + % `success = swpref.export_prefs` writes the preferences given in `obj` to + % the users home folder as `swprefs.json`. The file is in a basic `.json` + % format. + % + % ### See Also + % + % [swpref.import_prefs] + % + + obj = swpref; + obj.export(varargin{:}) + end + + + function obj = import_prefs(varargin) + % imports swpref object from file + % + % ### Syntax + % + % + % `obj = swpref.import_prefs` + % + % `obj = swpref.import_prefs(location)` + % + % ### Description + % + % `obj = swpref.import_prefs` loads the preferences given in by the file + % `swprefs.json` in the users home folder. It sets the preferences and + % returns a new preference object. + % + % `obj = swpref.import_prefs(location)` loads the preferences given in by the file + % specified by `location`, sets the preferences and returns a new + % preference object. + % + % ### See Also + % + % [swpref.export_prefs] + % + + obj = swpref; + obj.import(varargin{:}); + end + + end +end diff --git a/swfiles/sw_import.m b/swfiles/sw_import.m index ad83cec87..72feb4b82 100644 --- a/swfiles/sw_import.m +++ b/swfiles/sw_import.m @@ -26,8 +26,8 @@ swhelp sw_import return end - -fid = swpref.getpref('fid',true); +pref = swpref; +fid = pref.fid; if nargin < 2 toPlot = false; diff --git a/swfiles/sw_instrument.m b/swfiles/sw_instrument.m index 64009d4b9..eac9b659c 100644 --- a/swfiles/sw_instrument.m +++ b/swfiles/sw_instrument.m @@ -136,10 +136,11 @@ inpForm.soft = [inpForm.soft {false false false false false false}]; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; % Print output if param.fid == -1 - fid = swpref.getpref('fid',[]); + fid = pref.fid; else fid = param.fid; end diff --git a/swfiles/sw_model.m b/swfiles/sw_model.m index fecf7b279..4bcdc0fe5 100644 --- a/swfiles/sw_model.m +++ b/swfiles/sw_model.m @@ -42,8 +42,10 @@ return end +pref = swpref; + if nargin < 3 - fid = swpref.getpref('fid',true); + fid = pref.fid; end fprintf0(fid,'Preparing ''%s'' model ...\n',model); diff --git a/swfiles/sw_plotspec.m b/swfiles/sw_plotspec.m index 38af03e14..de34d9759 100644 --- a/swfiles/sw_plotspec.m +++ b/swfiles/sw_plotspec.m @@ -199,6 +199,7 @@ inpForm.size = [inpForm.size {[1 1] [1 1] [1 1] [1 2] [1 -7] }]; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; % plotmode string if numel(param.mode)>1 @@ -602,7 +603,7 @@ if nPlot>1 param.colormap = hsv2rgb([(1:nPlot)'/nPlot ones(nPlot,2)])'*255; else - param.colormap = {swpref.getpref('colormap',[])}; + param.colormap = {pref.colormap}; end end if ~iscell(param.colormap) diff --git a/swfiles/sw_qgrid.m b/swfiles/sw_qgrid.m index a6909cd09..bef79c9ca 100644 --- a/swfiles/sw_qgrid.m +++ b/swfiles/sw_qgrid.m @@ -85,6 +85,7 @@ inpForm.soft = [inpForm.soft {true true true false true true false}]; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; if nargin == 0 swhelp sw_qgrid @@ -92,7 +93,7 @@ end if param.fid == -1 - param.fid = swpref.getpref('fid',true); + param.fid = pref.fid; end % size of magnetic supercell diff --git a/swfiles/sw_timeit.m b/swfiles/sw_timeit.m index f2c6fdc4b..8188b7b50 100644 --- a/swfiles/sw_timeit.m +++ b/swfiles/sw_timeit.m @@ -40,6 +40,7 @@ function sw_timeit(percent,varargin) % global sw_time +pref = swpref; if nargin == 0 swhelp sw_timeit @@ -49,7 +50,7 @@ function sw_timeit(percent,varargin) if nargin > 2 && ~isempty(varargin{2}) && ~ischar(varargin{2}) fid = varargin{2}; else - fid = swpref.getpref('tid',[]); + fid = pref.tid; end if fid == 0 diff --git a/swfiles/sw_tofres.m b/swfiles/sw_tofres.m index 1f1ecd2db..535fdbe37 100644 --- a/swfiles/sw_tofres.m +++ b/swfiles/sw_tofres.m @@ -84,17 +84,18 @@ inpForm.size = {[1 -1] [1 -2] [1 -3] [1 1] [1 1]}; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; obj = spec.obj; if param.fid == -1 % Print output into the following file - fid = swpref.getpref('fid',true); + fid = pref.fid; else fid = param.fid; end if param.tid == -1 - param.tid = swpref.getpref('tid',[]); + param.tid = pref.tid; end hkl = spec.hkl; diff --git a/swfiles/swdoc.m b/swfiles/swdoc.m index 5ece8a425..e1d5e1b94 100644 --- a/swfiles/swdoc.m +++ b/swfiles/swdoc.m @@ -20,6 +20,8 @@ % documentation. % +pref = swpref; + if nargin == 0 funName0 = ''; end @@ -45,7 +47,7 @@ funName(funName=='.') = '_'; % open documentation using stored url + function name -docUrl = swpref.getpref('docurl',true); +docUrl = pref.docurl; if ~isempty(docUrl) && docUrl(end)~='/' docUrl = [docUrl '/']; end diff --git a/test/sw_test.m b/test/sw_test.m index 625e77080..6ca8b0e28 100644 --- a/test/sw_test.m +++ b/test/sw_test.m @@ -29,9 +29,10 @@ inpForm.size = {[1 1] [1 1] }; param = sw_readparam(inpForm, varargin{:}); +pref = swpref; if param.fid == -1 - param.fid = swpref.getpref('fid',true); + param.fid = pref.fid; else fid = param.fid; end diff --git a/tutorials/publish/publish_PSI.m b/tutorials/publish/publish_PSI.m index 8518493f2..a5f08bbcb 100644 --- a/tutorials/publish/publish_PSI.m +++ b/tutorials/publish/publish_PSI.m @@ -21,12 +21,13 @@ function publish_PSI(fName, dirName) % switch off text outputs from code and change to high resolution rendering % and start horace -fid0 = swpref.getpref('fid',[]); -tid0 = swpref.getpref('tid',[]); -nmesh0 = swpref.getpref('nmesh',[]); -npatch0 = swpref.getpref('npatch',[]); +pref = swpref; +fid0 = pref.fid; +tid0 = pref.tid; +nmesh0 = pref.nmesh; +npatch0 = pref.npatch; hor0 = horace; -swpref.setpref('fid',0,'tid',0,'nmesh',3,'npatch',50); +pref.set({'fid', 'tid', 'nmesh', 'npatch'},{0, 0, 3, 50}); % start horace horace('on') @@ -137,7 +138,7 @@ function publish_PSI(fName, dirName) end % restore SpinW and Horace state -swpref.setpref('fid',fid0,'tid',tid0,'nmesh',nmesh0,'npatch',npatch0); +pref.set({'fid', 'tid', 'nmesh', 'npatch'},{fid0, tid0, nmesh0, npatch0}); horace(hor0); end \ No newline at end of file diff --git a/tutorials/publish/tutorial30.m b/tutorials/publish/tutorial30.m index 55d5022ee..6d4a17d1a 100644 --- a/tutorials/publish/tutorial30.m +++ b/tutorials/publish/tutorial30.m @@ -23,7 +23,7 @@ % the default values, it can be defined in the startup.m file. To get all % current value use -swpref.getpref +swpref %% Generate lattice % We create a triangular lattice of magnetic atoms. diff --git a/tutorials/publish/tutorial33.m b/tutorials/publish/tutorial33.m index d7136b5ce..ff94b8e9d 100644 --- a/tutorials/publish/tutorial33.m +++ b/tutorials/publish/tutorial33.m @@ -27,8 +27,8 @@ % nmnesh=3 will make all sphere to have 1280 faces. npatch determines the % number of subdivision of the circle that is used to generate cylinders an % arrows (swplot.arrow and swplot.cylinder). - -swpref.setpref('nmesh',3,'npatch',50) +pref = swpref; +pref.set({'nmesh', 'npatch'}, {3, 50}) % The high level spinw.plot command calls lower level commands % (swplot.plotatom, swplot.plotion, swplot.plotbond, etc). For details diff --git a/tutorials/publish/tutorial35.m b/tutorials/publish/tutorial35.m index c9b2b68f3..267f02974 100644 --- a/tutorials/publish/tutorial35.m +++ b/tutorials/publish/tutorial35.m @@ -77,8 +77,8 @@ % https://goo.gl/8guwPF % Please save it with the name LuVO3_fitted_modes.txt into a location that % is visible to Matlab. - -swpref.setpref('tid',0) +perf = swpref; +pref.tid = 0; par_fit = struct; par_fit.datapath = 'LuVO3_fitted_modes.txt'; diff --git a/tutorials/publish/tutorial8.m b/tutorials/publish/tutorial8.m index d6893475f..16d945c5b 100644 --- a/tutorials/publish/tutorial8.m +++ b/tutorials/publish/tutorial8.m @@ -82,7 +82,7 @@ % only be calculated using the magnetic supercell. kag33Spec = AF33kagome.spinwave({[-1/2 0 0] [0 0 0] [1/2 1/2 0] 250},'hermit',false); -kag33Spec = sw_egrid(kag33Spec,'component','Sxx+Syy+Szz'); +kag33Spec = sw_egrid(kag33Spec,'component','Sxx+Syy+Szz','imagChk',false); figure subplot(2,1,1) sw_plotspec(kag33Spec,'mode',1,'axLim',[0 2.5],'colorbar',false',... diff --git a/tutorials/tutorial/YIG.m b/tutorials/tutorial/YIG.m index 9d2127073..50cd28105 100644 --- a/tutorials/tutorial/YIG.m +++ b/tutorials/tutorial/YIG.m @@ -101,11 +101,11 @@ caxis([-0.05 0.05]) %% powder spactrum - +pref = swpref; Q = linspace(0,4,101); E = linspace(0,30,501); nRand = 1e2; -swpref.setpref('tid',2); +pref.tid = 2; spec = yig.powspec(Q,'Evect',E,'nRand',nRand); @@ -114,7 +114,7 @@ Q = linspace(0,4,101); E = linspace(0,30,501); nRand = 1e3; -swpref.setpref('tid',0); +pref.tid = 0; profile on spec = tri.powspec(Q,'Evect',E,'nRand',nRand); profile off