Home > classes > @history > hist2m.m

hist2m

PURPOSE ^

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

SYNOPSIS ^

function cmds = hist2m(h)

DESCRIPTION ^

 HIST2M writes a new m-file that reproduces the analysis described in the
 history object.
 
   >> cmds = hist2m(h);
 
 M Hewitson 06-02-07

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function cmds = hist2m(h)
0002 
0003 % HIST2M writes a new m-file that reproduces the analysis described in the
0004 % history object.
0005 %
0006 %   >> cmds = hist2m(h);
0007 %
0008 % M Hewitson 06-02-07
0009 %
0010 
0011 ALGONAME = mfilename;
0012 VERSION  = '$Id: hist2m.m,v 1.11 2007/06/12 12:31:50 hewitson Exp $';
0013 
0014 NOW2INT = 1e7;  % factor to multiply now before taking floor to produce integer.
0015 
0016 % get a node list
0017 [n,a, nodes] = getNodes(h, [], 0, 1, []);
0018 
0019 % loop over nodes and convert to commands
0020 i = 1;
0021 cmds = {};
0022 while i <= length(nodes)
0023   v = nodes(i).n;
0024   idx = find([nodes(:).pn] == i);
0025   pl = nodes(i).pl;  
0026   h  = nodes(i).h;
0027   aoName = floor(rem(get(h, 'created'), 1)*NOW2INT);
0028   hi     = [nodes(idx).h];
0029   iNames = zeros(size(hi));
0030   for j=1:length(hi)
0031     iNames(j) =  floor(rem(get(hi(j), 'created'), 1)*NOW2INT);
0032   end
0033   cmd = writeCmd(char(nodes(i).names), pl, aoName, iNames);
0034 %   cmd = writeCmd(char(nodes(i).names), pl, aoName, idx);
0035   cmds = [cmds cellstr(cmd)];  
0036   i = i + 1;  
0037 end
0038 
0039 % now find commands that are duplicated after the '=' and remap those
0040 ncmds = length(cmds);
0041 for j = 1:ncmds
0042   cmdj = cmds{j};
0043   % now inspect all other commands prior to this one
0044   for k = j+1:ncmds
0045     cmdk = cmds{k};
0046     if strcmp(cmdj, cmdk)
0047       cmds{j} = '';
0048     end    
0049   end
0050 end
0051 
0052 % add the final command to produce a_out
0053 alast = deblank(strtok(cmds{1}, '='));
0054 cmds  = [cellstr(sprintf('a_out = %s;', alast)) cmds];
0055 
0056 %--------------------------------------------------------------------------
0057 %
0058 % write a command-line
0059 %
0060 function cmd = writeCmd(name, pl, aon, ains)
0061 
0062 ainsStr = '';
0063 % for i=length(ains):-1:1
0064 ni = length(ains);
0065 for i=1:ni
0066   ainsStr = [ainsStr sprintf('a%d, ', ains(i))];
0067 end
0068 
0069 name = strrep(name, '\_', '_');
0070 
0071 % look at the input parameters
0072 if isa(pl, 'plist')
0073   ps = writePlist(pl);
0074 else
0075   np = 0;
0076   ps = '';
0077 end
0078 if strcmp(ps, 'plist([])')
0079   ps = '';
0080 end
0081 pstr = deblank(sprintf('%s%s', ainsStr, ps));
0082 
0083 if ~isempty(pstr)
0084   if pstr(end) == ','
0085     pstr = pstr(1:end-1);
0086   end
0087 end
0088 cmd = sprintf('a%d = %s(%s);', aon, name, pstr);
0089 
0090 
0091 %-------------------------------------------------------
0092 function ps = writePlist(pl)
0093 
0094 ps = 'plist([';
0095 np = length(pl.params(:));
0096 for i=1:np
0097   p = pl.params(i);
0098   if ~isempty(p.val)
0099     if ischar(p.val)
0100       ps = [ps sprintf('param(''%s'', ''%s'') ', p.key, p.val)];
0101     elseif isnumeric(p.val)
0102       ps = [ps [sprintf('param(''%s'', [', p.key)  mat2str(p.val) ']) ']];
0103     elseif isa(p.val, 'specwin')
0104       w = p.val;
0105       wstr = string(w);
0106       ps = [ps sprintf('param(''%s'', %s) ', p.key, wstr) ];
0107     elseif isa(p.val, 'miir')
0108       f = p.val;
0109       fstr = string(f);
0110       ps = [ps sprintf('param(''%s'', %s) ', p.key, fstr) ];
0111     elseif isa(p.val, 'pzmodel')
0112       pzm = p.val;
0113       fstr = string(pzm);
0114       ps = [ps sprintf('param(''%s'', %s) ', p.key, fstr) ];
0115     else
0116       error(['### unknown parameter type: ' p.key '  ' p.val]);
0117     end
0118   end
0119 end
0120 ps = [ps '])'];
0121

Generated on Mon 02-Jul-2007 12:19:41 by m2html © 2003