|
1 |
| -function bcilab(varargin) |
2 |
| -% BCILAB startup function |
3 |
| -% |
4 |
| -% The startup function determines startup options according to a config file and then starts the |
5 |
| -% BCILAB toolbox based on config options. If a file named bcilab_config.m exists in the user's home |
6 |
| -% directory (under .bcilab) it will take precedence, otherwise the file of the same name in the |
7 |
| -% bcilab folder will be used. The file name (or path) can also be specified directly as the first |
8 |
| -% argument to this function. |
9 |
| -% |
10 |
| -% In addition, startup options can also be passed directly to this function, in the form of name- |
11 |
| -% value pairs. These options selectively override those that are specified in the config file. |
12 |
| -% |
13 |
| -% Generally, the way to load bcilab is to cd into the BCILAB path, and execute this function. |
14 |
| -% The BCILAB path should not be added to the default MATLAB path, especially *never* recursively. |
15 |
| -% |
16 |
| -% In: |
17 |
| -% ConfigScript : optional file name of a configuration script (assigning values to config |
18 |
| -% variables, see bcilab_config.m for an example) |
19 |
| -% |
20 |
| -% Options... : optional name-value pairs to override options; for possible options, |
21 |
| -% see env_startup |
22 |
| -% |
23 |
| -% Notes: |
24 |
| -% The config files are regular MATLAB script and may include conditional statements (depending on |
25 |
| -% the platform used), etc. However, to allow modification of these files via the GUI, it is |
26 |
| -% recommended to keep them simple. |
27 |
| -% |
28 |
| -% Examples: |
29 |
| -% % start the toolbox using default settings (found in bcilab_config.m) |
30 |
| -% cd /my/path/to/bcilab-0.9-beta2b; bcilab |
31 |
| -% |
32 |
| -% % start the toolbox, passing a particular config script |
33 |
| -% cd /my/path/to/bcilab-0.9-beta2b; bcilab('myconfig.m') |
34 |
| -% |
35 |
| -% % start the toolbox, passing a particular config script, and also override a few parameters directly |
36 |
| -% cd /my/path/to/bcilab-0.9-beta2b; bcilab('myconfig.m','data','/data','worker',true) |
37 |
| -% |
38 |
| -% See also: |
39 |
| -% env_startup |
40 |
| -% |
41 |
| -% Christian Kothe, Swartz Center for Computational Neuroscience, UCSD |
42 |
| -% 2010-10-30 |
43 |
| - |
44 |
| -disp('starting BCILAB...'); |
45 |
| -if mod(nargin,2) == 0 |
46 |
| - % an even number of arguments (or none) was passed -- therefore, no config script was given, |
47 |
| - % so assume the default one |
48 |
| - configscript = 'bcilab_config'; |
49 |
| -else |
50 |
| - % an odd number of arguments was passed |
51 |
| - % --> the first argument is the name of the config script |
52 |
| - if ~ischar(varargin{1}) |
53 |
| - error('If an odd number of parameters is provided then the first parameter must be the name of a config script.'); end |
54 |
| - configscript = varargin{1}; |
55 |
| - % ... and the rest are the options. |
56 |
| - varargin = varargin(2:end); |
57 |
| -end |
58 |
| -if ~iscellstr(varargin(1:2:end)) |
59 |
| - error('The arguments to bcilab.m must be name-value pairs.'); end |
60 |
| - |
61 |
| -% check if the user's home directory (.bcilab sub-directory) contains a bcilab_config.m, and prefer |
62 |
| -% that if present |
63 |
| -home = char(java.lang.System.getProperty('user.home')); |
64 |
| -if exist([home filesep '.bcilab' filesep configscript '.m'],'file') |
65 |
| - configscript = [home filesep '.bcilab' filesep configscript '.m']; end |
66 |
| - |
67 |
| -if isdeployed && ~isempty(varargin) |
68 |
| - try |
69 |
| - varargin = cellfun(@eval,varargin,'UniformOutput',false); |
70 |
| - catch |
71 |
| - disp('When passing command-line arguments, make sure that strings are enclosed in '''' characters.'); |
72 |
| - disp('Otherwise, all of them will be assumed to be strings.'); |
73 |
| - end |
74 |
| -end |
75 |
| - |
76 |
| - |
77 |
| -% sanitize and run the config script |
78 |
| -if ~any(configscript == filesep) |
79 |
| - % relative path given |
80 |
| - if isdeployed |
81 |
| - if ~any(configscript == '.') |
82 |
| - configscript = [configscript '.m']; end |
83 |
| - % we assume that the configscript lies in a directory at or above the binary |
84 |
| - tmpdir = [pwd filesep]; |
85 |
| - delims = strfind(tmpdir,filesep); |
86 |
| - for k=length(delims):-1:1 |
87 |
| - scriptpath = [tmpdir(1:delims(k)) configscript]; |
88 |
| - if exist(scriptpath,'file') |
89 |
| - configscript = scriptpath; |
90 |
| - break; |
91 |
| - end |
92 |
| - end |
93 |
| - if ~exist(scriptpath,'file') |
94 |
| - error('Config script %s not found in any path relative to the binary.',configscript); end |
95 |
| - else |
96 |
| - configscript = which(configscript); |
97 |
| - end |
98 |
| -end |
99 |
| -fprintf('running config script %s...\n',configscript); |
100 |
| -addpath([fileparts(mfilename('fullpath')) filesep 'code' filesep 'misc']); |
101 |
| -run_script(configscript); |
102 |
| - |
103 |
| -% collect the variables as defined in the config script into a struct |
104 |
| -infos = whos(); |
105 |
| -for n = {infos.name} |
106 |
| - settings.(n{1}) = eval(n{1}); end |
107 |
| - |
108 |
| -% load the toolbox (using code/environment/env_startup) |
109 |
| -% (using the settings from the config script, possibly overridden by name-value pairs in varargin) |
110 |
| -disp('running startup function...'); |
111 |
| -if ~isdeployed |
112 |
| - cd([fileparts(mfilename('fullpath')) filesep 'code' filesep 'environment']); end |
113 |
| - |
114 |
| -try |
115 |
| - env_startup(settings,varargin{:},'configscript',configscript); |
116 |
| -catch |
117 |
| - % store a crash report for debugging in deployed cases |
118 |
| - report.settings = settings; |
119 |
| - report.varargin = varargin; |
120 |
| - report.lasterror = lasterror; %#ok<*LERR> |
121 |
| - save bcilab_crashreport report |
122 |
| - rethrow(lasterror); |
123 |
| -end |
| 1 | +function bcilab(varargin) |
| 2 | +% BCILAB startup function |
| 3 | +% |
| 4 | +% The startup function determines startup options according to a config file and then starts the |
| 5 | +% BCILAB toolbox based on config options. If a file named bcilab_config.m exists in the user's home |
| 6 | +% directory (under .bcilab) it will take precedence, otherwise the file of the same name in the |
| 7 | +% bcilab folder will be used. The file name (or path) can also be specified directly as the first |
| 8 | +% argument to this function. |
| 9 | +% |
| 10 | +% In addition, startup options can also be passed directly to this function, in the form of name- |
| 11 | +% value pairs. These options selectively override those that are specified in the config file. |
| 12 | +% |
| 13 | +% Generally, the way to load bcilab is to cd into the BCILAB path, and execute this function. |
| 14 | +% The BCILAB path should not be added to the default MATLAB path, especially *never* recursively. |
| 15 | +% |
| 16 | +% In: |
| 17 | +% ConfigScript : optional file name of a configuration script (assigning values to config |
| 18 | +% variables, see bcilab_config.m for an example) |
| 19 | +% |
| 20 | +% Options... : optional name-value pairs to override options; for possible options, |
| 21 | +% see env_startup |
| 22 | +% |
| 23 | +% Notes: |
| 24 | +% The config files are regular MATLAB script and may include conditional statements (depending on |
| 25 | +% the platform used), etc. However, to allow modification of these files via the GUI, it is |
| 26 | +% recommended to keep them simple. |
| 27 | +% |
| 28 | +% Examples: |
| 29 | +% % start the toolbox using default settings (found in bcilab_config.m) |
| 30 | +% cd /my/path/to/bcilab-0.9-beta2b; bcilab |
| 31 | +% |
| 32 | +% % start the toolbox, passing a particular config script |
| 33 | +% cd /my/path/to/bcilab-0.9-beta2b; bcilab('myconfig.m') |
| 34 | +% |
| 35 | +% % start the toolbox, passing a particular config script, and also override a few parameters directly |
| 36 | +% cd /my/path/to/bcilab-0.9-beta2b; bcilab('myconfig.m','data','/data','worker',true) |
| 37 | +% |
| 38 | +% See also: |
| 39 | +% env_startup |
| 40 | +% |
| 41 | +% Christian Kothe, Swartz Center for Computational Neuroscience, UCSD |
| 42 | +% 2010-10-30 |
| 43 | + |
| 44 | +disp('starting BCILAB...'); |
| 45 | +if mod(nargin,2) == 0 |
| 46 | + % an even number of arguments (or none) was passed -- therefore, no config script was given, |
| 47 | + % so assume the default one |
| 48 | + configscript = 'bcilab_config'; |
| 49 | +else |
| 50 | + % an odd number of arguments was passed |
| 51 | + % --> the first argument is the name of the config script |
| 52 | + if ~ischar(varargin{1}) |
| 53 | + error('If an odd number of parameters is provided then the first parameter must be the name of a config script.'); end |
| 54 | + configscript = varargin{1}; |
| 55 | + % ... and the rest are the options. |
| 56 | + varargin = varargin(2:end); |
| 57 | +end |
| 58 | +if ~iscellstr(varargin(1:2:end)) |
| 59 | + error('The arguments to bcilab.m must be name-value pairs.'); end |
| 60 | + |
| 61 | +% check if the user's home directory (.bcilab sub-directory) contains a bcilab_config.m, and prefer |
| 62 | +% that if present |
| 63 | +home = char(java.lang.System.getProperty('user.home')); |
| 64 | +if exist([home filesep '.bcilab' filesep configscript '.m'],'file') |
| 65 | + configscript = [home filesep '.bcilab' filesep configscript '.m']; end |
| 66 | + |
| 67 | +if isdeployed && ~isempty(varargin) |
| 68 | + try |
| 69 | + varargin = cellfun(@eval,varargin,'UniformOutput',false); |
| 70 | + catch |
| 71 | + disp('When passing command-line arguments, make sure that strings are enclosed in '''' characters.'); |
| 72 | + disp('Otherwise, all of them will be assumed to be strings.'); |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | + |
| 77 | +% sanitize and run the config script |
| 78 | +if ~any(configscript == filesep) |
| 79 | + % relative path given |
| 80 | + if isdeployed |
| 81 | + if ~any(configscript == '.') |
| 82 | + configscript = [configscript '.m']; end |
| 83 | + % we assume that the configscript lies in a directory at or above the binary |
| 84 | + tmpdir = [pwd filesep]; |
| 85 | + delims = strfind(tmpdir,filesep); |
| 86 | + for k=length(delims):-1:1 |
| 87 | + scriptpath = [tmpdir(1:delims(k)) configscript]; |
| 88 | + if exist(scriptpath,'file') |
| 89 | + configscript = scriptpath; |
| 90 | + break; |
| 91 | + end |
| 92 | + end |
| 93 | + if ~exist(scriptpath,'file') |
| 94 | + error('Config script %s not found in any path relative to the binary.',configscript); end |
| 95 | + else |
| 96 | + configscript = which(configscript); |
| 97 | + end |
| 98 | +end |
| 99 | +fprintf('running config script %s...\n',configscript); |
| 100 | +addpath([fileparts(mfilename('fullpath')) filesep 'code' filesep 'misc']); |
| 101 | +run_script(configscript); |
| 102 | + |
| 103 | +% collect the variables as defined in the config script into a struct |
| 104 | +infos = whos(); |
| 105 | +for n = {infos.name} |
| 106 | + settings.(n{1}) = eval(n{1}); end |
| 107 | + |
| 108 | +% load the toolbox (using code/environment/env_startup) |
| 109 | +% (using the settings from the config script, possibly overridden by name-value pairs in varargin) |
| 110 | +disp('running startup function...'); |
| 111 | +if ~isdeployed |
| 112 | + cd([fileparts(mfilename('fullpath')) filesep 'code' filesep 'environment']); end |
| 113 | + |
| 114 | +try |
| 115 | + env_startup(settings,varargin{:},'configscript',configscript); |
| 116 | +catch |
| 117 | + % store a crash report for debugging in deployed cases |
| 118 | + report.settings = settings; |
| 119 | + report.varargin = varargin; |
| 120 | + report.lasterror = lasterror; %#ok<*LERR> |
| 121 | + save bcilab_crashreport report |
| 122 | + rethrow(lasterror); |
| 123 | +end |
0 commit comments