Home > functions > gui > project_mgr_dir > project_file_mgr.m

project_file_mgr

PURPOSE ^

This function manages project file.

SYNOPSIS ^

function [varargout] = project_file_mgr(varargin)

DESCRIPTION ^

 This function manages project file.
 Usage :
  key : 'make', 'load', 'add', 'get_all_parameters', 'get_parameter',
        'get_project_root', 'get_project_name', 'get_project_file'

  project_file_mgr('make', <filename>, <project_name>, <project_root>);
    projec file create. 

  project_file_mgr('load', <filename>); 
    load project file on memory.

  project_file_mgr('add', <parm_name>, <parm>);
    save parameter to file. you should 'load' project file before this operation.
    e.g project_file_mgr('add', 'brain_parm', brain_parm);

  project_file_mgr('get_all_parameters');
    return [parm, parm_name]
                parm : parameter list(Nx1)
           parm_name : parameter name list(Nx1)
    parameter order is sorted by timestamp(registered order.)

  project_file_mgr('get_parameter', <itemNo>)
    return [parm, parm_name]
                parm : parameter
           parm_name : parameter name
    itemNo : registered order.

  project_file_mgr('get_project_root');
    return proj_root

  project_file_mgr('get_project_name');
    return project_name

  project_file_mgr('get_project_file');
    return project file name loaded on memory.

 [NOTE]
  associate with project_mgr :
   project_file_mgr notify to the project_mgr when the status(persistent value)
   changed. And then, project_mgr receives this notify, project_mgr calls 
   project_file_mgr to get updated parameters.


 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [varargout] = project_file_mgr(varargin)
0002 % This function manages project file.
0003 % Usage :
0004 %  key : 'make', 'load', 'add', 'get_all_parameters', 'get_parameter',
0005 %        'get_project_root', 'get_project_name', 'get_project_file'
0006 %
0007 %  project_file_mgr('make', <filename>, <project_name>, <project_root>);
0008 %    projec file create.
0009 %
0010 %  project_file_mgr('load', <filename>);
0011 %    load project file on memory.
0012 %
0013 %  project_file_mgr('add', <parm_name>, <parm>);
0014 %    save parameter to file. you should 'load' project file before this operation.
0015 %    e.g project_file_mgr('add', 'brain_parm', brain_parm);
0016 %
0017 %  project_file_mgr('get_all_parameters');
0018 %    return [parm, parm_name]
0019 %                parm : parameter list(Nx1)
0020 %           parm_name : parameter name list(Nx1)
0021 %    parameter order is sorted by timestamp(registered order.)
0022 %
0023 %  project_file_mgr('get_parameter', <itemNo>)
0024 %    return [parm, parm_name]
0025 %                parm : parameter
0026 %           parm_name : parameter name
0027 %    itemNo : registered order.
0028 %
0029 %  project_file_mgr('get_project_root');
0030 %    return proj_root
0031 %
0032 %  project_file_mgr('get_project_name');
0033 %    return project_name
0034 %
0035 %  project_file_mgr('get_project_file');
0036 %    return project file name loaded on memory.
0037 %
0038 % [NOTE]
0039 %  associate with project_mgr :
0040 %   project_file_mgr notify to the project_mgr when the status(persistent value)
0041 %   changed. And then, project_mgr receives this notify, project_mgr calls
0042 %   project_file_mgr to get updated parameters.
0043 %
0044 %
0045 % Copyright (C) 2011, ATR All Rights Reserved.
0046 % License : New BSD License(see VBMEG_LICENSE.txt)
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 % File operation function
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             % *_parm : cell is loaded.
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         % remove empty line
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         % parameter sort by time_stamp
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     % caller_function is supposed to the name of job function.
0135     stack = dbstack;
0136     [a, caller_function] = vb_get_file_parts(stack(2).name);
0137     
0138     parm_name = varargin{2};
0139     parm = varargin{3};
0140     
0141     % Add execution information
0142     parm.time_stamp      = datestr(fix(clock), 31);
0143     parm.caller_function = caller_function;
0144     
0145     Nparm = 0;
0146     tmp = whos('-file', project_file, parm_name);
0147     if length(tmp)
0148         load(project_file, parm_name);
0149         Nparm = length(eval(parm_name)); % Nparm = length(brain_parm)
0150     else
0151         command = [parm_name '=cell(0);'];
0152         eval(command);
0153     end
0154     command = [parm_name '{' num2str(Nparm+1) '} = parm;'];
0155     eval(command);
0156     
0157     vb_save(project_file, parm_name);
0158     project_file_mgr('load', project_file);
0159     notify_status_change;
0160     varargout{1} = true;
0161 
0162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0163 % Parameters get function
0164 %
0165 case 'get_all_parameters'
0166     % return sorted data
0167     varargout{1} = all_parms;
0168     varargout{2} = parm_types;
0169 
0170 case 'get_parameter'
0171     itemNum = varargin{2};
0172     [all_parms, parm_types] = project_file_mgr('get_all_parameters');
0173     if length(all_parms) >= itemNum
0174         varargout{1} = all_parms{itemNum};
0175         varargout{2} = parm_types{itemNum};
0176     else
0177         varargout{1} = [];
0178         varargout{2} = [];
0179     end
0180 
0181 case 'get_project_root'
0182     varargout{1} = proj_root;
0183 case 'get_project_name'
0184     varargout{1} = project_name;
0185 case 'get_project_file'
0186     varargout{1} = project_file;
0187 end
0188 
0189 function notify_status_change
0190     current_hidden_state = get(0, 'ShowHiddenHandles');
0191     set(0, 'ShowHiddenHandles', 'on');
0192     fig = findobj('Tag', 'project_mgr');
0193     set(0, 'ShowHiddenHandles', current_hidden_state);
0194     if ~isempty(fig)
0195         project_mgr_view_update(fig);
0196     end

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005