This repository has been archived by the owner on Mar 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
install_spinw.m
171 lines (147 loc) · 4.57 KB
/
install_spinw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function install_spinw(varargin)
% installs SpinW
%
% INSTALL_SPINW()
%
% It adds the SpinW folder to the search path and modifies startup.m and
% clears the class definitions. There is a y/n question before every
% operation.
%
% if nargin == 0
% swhelp sw_install
% return
% end
silent = false;
if nargin == 2
if strcmpi(varargin{1},'silent')
if isa(varargin{2},'logical')
silent = varargin{2};
end
end
end
newline = char(10); %#ok<CHARTEN>
% remove old SpinW installation from path
fprintf('\nRemoving path to old SpinW installation if exists!\n')
try
rmpath(genpath(sw_rootdir));
end
% find SpinW folder
folName = fileparts(mfilename('fullpath'));
% adding new path
fprintf('Adding path to new SpinW installation: %s!\n',folName);
ww = warning;
warning('off');
addpath(genpath(folName));
warning(ww);
% remove files aren't needed for new Matlab versions
% functions introduced in R2014a
if ~verLessThan('matlab', '8.1')
% strjoin()
fList = dir([folName filesep 'external' filesep 'strjoin*']);
for ii = 1:numel(fList)
delete([folName filesep 'external' filesep fList(ii).name]);
end
% strsplit
fList = dir([folName filesep 'external' filesep 'strsplit*']);
for ii = 1:numel(fList)
delete([folName filesep 'external' filesep fList(ii).name]);
end
% gobjects()
fList = dir([folName filesep 'external' filesep 'gobjects*']);
for ii = 1:numel(fList)
delete([folName filesep 'external' filesep fList(ii).name]);
end
else
% rename the functions to be used in Matlab versions prior to R2014a
% strjoin()
try %#ok<*TRYNC>
movefile([folName filesep 'external' filesep 'strjoin0.m'],...
[folName filesep 'external' filesep 'strjoin.m']);
end
% strsplit()
try
movefile([folName filesep 'external' filesep 'strsplit0.m'],...
[folName filesep 'external' filesep 'strsplit.m']);
end
% gobjects()
try
movefile([folName filesep 'external' filesep 'gobjects0.m'],...
[folName filesep 'external' filesep 'gobjects.m']);
end
end
% functions introduced in R2015a
% if ~verLessThan('matlab', '8.5')
% % uniquetol()
% fList = dir([folName filesep 'external' filesep 'uniquetol*']);
% for ii = 1:numel(fList)
% delete([folName filesep 'external' filesep fList(ii).name]);
% end
% end
fprintf(['In order to reach SpinW after restarting Matlab, the following\n'...
'line has to be added to your startup.m file:\n']);
fprintf(' addpath(genpath(''%s''));\n',folName);
% location of Matlab startup file
sfLoc = which('startup');
uPath = userpath;
% remove ':' and ';' characters from the userpath
uPath = [uPath(~ismember(uPath,';')) filesep 'startup.m'];
% create new startup.m file
if isempty(sfLoc)
if ~silent
answer = getinput(sprintf(['You don''t have a Matlab startup.m file,\n'...
'do you want it to be created at %s? (y/n)'],esc(uPath)),'yn');
else
answer = 'n';
end
if answer == 'y'
fclose(fopen(uPath,'w'));
sfLoc = uPath;
end
end
if ~isempty(sfLoc)
if ~silent
answer = getinput(['Would you like to add the following line:' newline...
'addpath(genpath(''' esc(folName) '''));' newline 'to the end of '...
'your Matlab startup file (' esc(sfLoc) ')? (y/n)'],'yn');
else
answer = 'n';
end
if answer == 'y'
fid = fopen(sfLoc,'a');
fprintf(fid,['\n%%###SW_UPDATE\n%% Path to the SpinW installation\n'...
'addpath(genpath(''' esc(folName) '''));\n%%###SW_UPDATE\n']);
fclose(fid);
end
end
if ~silent
answer = getinput(...
['\nIn order to refresh the internal class definitions of Matlab (to\n'...
'access the new SpinW version), issuing the "clear classes" command\n'...
'is necessary. However this command will also clear all your variables\n'...
'in the Matlab internal memory. Would you like the updater to issue\n'...
'the command now, otherwise you can do it manually later.\n'...
'Do you want to issue the command "clear classes" now? (y/n)'],'yn');
else
answer = 'n';
end
if answer == 'y'
clear('classes'); %#ok<CLCLS>
disp('Matlab class memory is refreshed!')
end
disp('The installation of SpinW was successful!')
end
function str = esc(str)
% escape \ characters
str = regexprep(str,'\\','\\\\');
end
function answer = getinput(message,good)
% get the necessary letter input
answer = ' ';
while ~ismember(answer(1),good)
answer = input(message,'s');
if isempty(answer)
answer = 0;
end
end
answer = answer(1);
end