0001 function [varargout] = project_file_mgr(varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 mlock;
0048 persistent project_name;
0049 persistent proj_root;
0050 persistent project_file;
0051
0052 persistent all_parms;
0053 persistent parm_types;
0054
0055 key = varargin{1};
0056
0057 switch(key)
0058
0059
0060
0061
0062
0063 case 'make'
0064 try
0065 project_file = varargin{2};
0066 project_name = varargin{3};
0067 proj_root = varargin{4};
0068 vb_fsave(project_file, 'project_name', 'proj_root');
0069 notify_status_change;
0070 varargout{1} = false;
0071 catch
0072 varargout{1} = true;
0073 end
0074
0075 case 'load'
0076 err = false;
0077 project_file = varargin{2};
0078 if exist(project_file, 'file')
0079 all_parms = [];
0080 parm_types = [];
0081 load(project_file, 'project_name', 'proj_root');
0082 contains = whos('-file', project_file, '*_parm');
0083 for k=1:length(contains)
0084 parm_name = contains(k).name;
0085
0086 tmp = load(project_file, parm_name);
0087 if ~iscell(tmp.(parm_name))
0088 parm = cell(1);
0089 parm{1} = tmp.parm_name;
0090 else
0091 parm = tmp.(parm_name);
0092 end
0093 all_parms = [all_parms; parm(:)];
0094 parm_types = [parm_types; cellstr(repmat(parm_name, length(parm), 1))];
0095 end
0096
0097
0098 empty_ix = [];
0099 for k=1:length(parm_types)
0100 if isempty(parm_types{k})
0101 empty_ix = [empty_ix;k];
0102 end
0103 end
0104 if ~isempty(empty_ix)
0105 parm_types(empty_ix) = [];
0106 end
0107
0108
0109 time_stamps = [];
0110 for k=1:length(all_parms)
0111 if ~isfield(all_parms{k}, 'time_stamp')
0112 break;
0113 end
0114 time_stamps = [time_stamps; all_parms{k}.time_stamp];
0115 end
0116 if ~isempty(time_stamps)
0117 [time_stamps, ix] = sortrows(time_stamps);
0118 all_parms = all_parms(ix);
0119 parm_types = parm_types(ix);
0120 end
0121
0122 notify_status_change;
0123 err = false;
0124 else
0125 err = true;
0126 end
0127 varargout{1} = err;
0128
0129 case 'add'
0130 if isempty(project_file) || ~exist(project_file, 'file')
0131 varargout{1} = false;
0132 return;
0133 end
0134
0135 stack = dbstack;
0136 [a, caller_function] = vb_get_file_parts(stack(2).name);
0137 if nargin == 4
0138 caller_function = varargin{4};
0139 end
0140 parm_name = varargin{2};
0141 parm = varargin{3};
0142
0143
0144 parm.time_stamp = datestr(fix(clock), 31);
0145 parm.caller_function = caller_function;
0146
0147 Nparm = 0;
0148 tmp = whos('-file', project_file, parm_name);
0149 if length(tmp)
0150 load(project_file, parm_name);
0151 Nparm = length(eval(parm_name));
0152 else
0153 command = [parm_name '=cell(0);'];
0154 eval(command);
0155 end
0156 command = [parm_name '{' num2str(Nparm+1) '} = parm;'];
0157 eval(command);
0158
0159 vb_save(project_file, parm_name);
0160 project_file_mgr('load', project_file);
0161 notify_status_change;
0162 varargout{1} = true;
0163
0164
0165
0166
0167 case 'get_all_parameters'
0168
0169 varargout{1} = all_parms;
0170 varargout{2} = parm_types;
0171
0172 case 'get_parameter'
0173 itemNum = varargin{2};
0174 [all_parms, parm_types] = project_file_mgr('get_all_parameters');
0175 if length(all_parms) >= itemNum
0176 varargout{1} = all_parms{itemNum};
0177 varargout{2} = parm_types{itemNum};
0178 else
0179 varargout{1} = [];
0180 varargout{2} = [];
0181 end
0182
0183 case 'get_project_root'
0184 varargout{1} = proj_root;
0185 case 'get_project_name'
0186 varargout{1} = project_name;
0187 case 'get_project_file'
0188 varargout{1} = project_file;
0189 end
0190
0191 function notify_status_change
0192 current_hidden_state = get(0, 'ShowHiddenHandles');
0193 set(0, 'ShowHiddenHandles', 'on');
0194 fig = findobj('Tag', 'project_mgr');
0195 set(0, 'ShowHiddenHandles', current_hidden_state);
0196 if ~isempty(fig)
0197 project_mgr_view_update(fig);
0198 end