Home > classes > @history > hist2m.m

hist2m

PURPOSE ^

HIST2M writes a new m-file that reproduces the analysis described in the history object.

SYNOPSIS ^

function cmds = hist2m(varargin)

DESCRIPTION ^

 HIST2M writes a new m-file that reproduces the analysis described in the history object.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: HIST2M writes a new m-file that reproduces the analysis described
              in the history object.

 CALL:        cmds = hist2m(h);

 INPUT:       h    - history object

 OUTPUT:      cmds - cell array with the commands to reproduce the data

 VERSION:     $Id: hist2m.html,v 1.14 2008/03/31 10:27:38 hewitson Exp $

 HISTORY:     06-02-2007 M Hewitson
                 Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function cmds = hist2m(varargin)
0002 % HIST2M writes a new m-file that reproduces the analysis described in the history object.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: HIST2M writes a new m-file that reproduces the analysis described
0007 %              in the history object.
0008 %
0009 % CALL:        cmds = hist2m(h);
0010 %
0011 % INPUT:       h    - history object
0012 %
0013 % OUTPUT:      cmds - cell array with the commands to reproduce the data
0014 %
0015 % VERSION:     $Id: hist2m.html,v 1.14 2008/03/31 10:27:38 hewitson Exp $
0016 %
0017 % HISTORY:     06-02-2007 M Hewitson
0018 %                 Creation
0019 %
0020 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0021 
0022 ALGONAME = mfilename;
0023 VERSION  = '$Id: hist2m.html,v 1.14 2008/03/31 10:27:38 hewitson Exp $';
0024 CATEGORY = 'Output';
0025 
0026 % Check if this is a call for parameters
0027 if nargin == 2
0028   if isa(varargin{1}, 'history') && ischar(varargin{2})
0029     in = char(varargin{1});
0030     if strcmp(in, 'Params')
0031       cmds = plist;
0032       return
0033     elseif strcmp(in, 'Version')
0034       cmds = VERSION;
0035       return
0036     elseif strcmp(in, 'Category')
0037       cmds = CATEGORY;
0038       return
0039     end
0040   end
0041 end
0042 
0043 % Set inputs
0044 h = varargin{1};
0045 
0046 % Created contains the epochtime in millisecounds and CREATED2INT define
0047 % the number of the last digits
0048 CREATED2INT = 1e7;
0049 
0050 % get a node list
0051 disp(' -- extracting node list from history');
0052 [n,a, nodes] = getNodes(h, [], 0, 1, []);
0053 
0054 disp(' -- converting history nodes to commands');
0055 % loop over nodes and convert to commands
0056 i = 1;
0057 cmds = {};
0058 while i <= length(nodes)
0059   v      = nodes(i).n;
0060   idx    = find([nodes(:).pn] == i);
0061   pl     = nodes(i).pl;
0062   h      = nodes(i).h;
0063   aoName = mod(h.created.utc_epoch_milli,CREATED2INT);
0064   hi     = [nodes(idx).h];
0065   iNames = zeros(size(hi));
0066   for j=1:length(hi)
0067     created = hi(j).created; %get(hi(j), 'created');
0068     iNames(j) =  mod(created.utc_epoch_milli,CREATED2INT);
0069   end
0070   cmd = writeCmd(char(nodes(i).names), pl, aoName, iNames);
0071   disp(sprintf(' -- wrote command for node %d [%s]', i, char(nodes(i).names)));
0072   cmds = [cmds cellstr(cmd)];
0073   i = i + 1;
0074 end
0075 
0076 % now find commands that are duplicated after the '=' and remap those
0077 disp(' -- fixing duplicate commands');
0078 ncmds = length(cmds);
0079 for j = 1:ncmds
0080   cmdj = cmds{j};
0081   % now inspect all other commands prior to this one
0082   for k = j+1:ncmds
0083     cmdk = cmds{k};
0084     if strcmp(cmdj, cmdk)
0085       cmds{j} = '';
0086     end
0087   end
0088 end
0089 
0090 disp(' -- writing output line');
0091 % add the final command to produce a_out
0092 alast = deblank(strtok(cmds{1}, '='));
0093 cmds  = [cellstr(sprintf('a_out = %s;', alast)) cmds];
0094 
0095 %--------------------------------------------------------------------------
0096 %
0097 % write a command-line
0098 %
0099 function cmd = writeCmd(name, pl, aon, ains)
0100 
0101 ainsStr = '';
0102 % for i=length(ains):-1:1
0103 ni = length(ains);
0104 for i=1:ni
0105   ainsStr = [ainsStr sprintf('a%d, ', ains(i))];
0106 end
0107 
0108 name = strrep(name, '\_', '_');
0109 
0110 % look at the input parameters
0111 if isa(pl, 'plist')
0112   ps = writePlist(pl);
0113 else
0114   np = 0;
0115   ps = '';
0116 end
0117 if strcmp(ps, 'plist([])')
0118   ps = '';
0119 end
0120 pstr = deblank(sprintf('%s%s', ainsStr, ps));
0121 
0122 if ~isempty(pstr)
0123   if pstr(end) == ','
0124     pstr = pstr(1:end-1);
0125   end
0126 end
0127 cmd = sprintf('a%d = %s(%s);', aon, name, pstr);
0128 
0129 
0130 %-------------------------------------------------------
0131 function ps = writePlist(pl)
0132 
0133 ps = 'plist([';
0134 np = length(pl.params(:));
0135 for i=1:np
0136   p = pl.params(i);
0137   if ~isempty(p.val)
0138 
0139     %%%   char   %%%
0140     if ischar(p.val)
0141       ps = [ps sprintf('param(''%s'', ''%s'') ', p.key, p.val)];
0142 
0143     %%%   numeric   %%%
0144     elseif isnumeric(p.val)
0145       ps = [ps [sprintf('param(''%s'', [', p.key)  ltpda_mat2str(p.val) ']) ']];
0146 
0147     %%%   specwin   %%%
0148     elseif isa(p.val, 'specwin')
0149       w = p.val;
0150       wstr = string(w);
0151       ps = [ps sprintf('param(''%s'', %s) ', p.key, wstr) ];
0152 
0153     %%%   miir   %%%
0154     elseif isa(p.val, 'miir')
0155       f = p.val;
0156       fstr = string(f);
0157       ps = [ps sprintf('param(''%s'', %s) ', p.key, fstr) ];
0158 
0159     %%%   pzmodel   %%%
0160     elseif isa(p.val, 'pzmodel')
0161       pzm = p.val;
0162       fstr = string(pzm);
0163       ps = [ps sprintf('param(''%s'', %s) ', p.key, fstr) ];
0164 
0165     %%%   time   %%%
0166     elseif isa(p.val, 'time')
0167       tt = p.val;
0168       fstr = string(tt);
0169       ps = [ps sprintf('param(''%s'', %s) ', p.key, fstr) ];
0170 
0171     %%%   provenance   %%%
0172     elseif isa(p.val, 'provenance')
0173 
0174     %%%   history   %%%
0175     elseif isa(p.val, 'history')
0176       
0177     %%%   plist   %%%
0178     elseif isa(p.val, 'plist')
0179       fstr = string(p.val);
0180       ps = [ps sprintf('%s', fstr) ];
0181     else
0182       error(['### unknown parameter type: ' p.key '  ' class(p.val)]);
0183     end
0184   end
0185 end
0186 ps = [ps '])'];
0187

Generated on Mon 31-Mar-2008 12:20:24 by m2html © 2003