Index: /issm/trunk/src/m/solutions/dakota/dakota_out_parse.m
===================================================================
--- /issm/trunk/src/m/solutions/dakota/dakota_out_parse.m	(revision 455)
+++ /issm/trunk/src/m/solutions/dakota/dakota_out_parse.m	(revision 456)
@@ -47,8 +47,8 @@
 display(sprintf('Dakota method=%s.',method));
 
-scm =[];
-pcm =[];
-srcm=[];
-prcm=[];
+scm =struct([]);
+pcm =struct([]);
+srcm=struct([]);
+prcm=struct([]);
 
 %%  switch according to the Dakota method
@@ -175,7 +175,45 @@
 function [dresp,scm,pcm,srcm,prcm]=nond_samp(fidi)
 
-ieof=0;
-
-%%  loop through the file to find the Dakota statistics
+%  loop through the file to find the number of function evaluations
+
+[nfeval]=nfeval_read(fidi);
+
+%  loop through the file to find the number of samples
+
+[nsamp]=nsamp_read(fidi);
+
+%  loop through the file to find the moments
+
+[dresp]=moments_read(fidi,struct([]));
+
+%  loop through the file to find the confidence intervals
+
+[dresp]=cis_read(fidi,dresp);
+
+%  loop through the file to find the probabilities
+
+[dresp]=cdfs_read(fidi,dresp);
+
+%  loop through the file to find the scm
+
+[scm]=corrmat_read(fidi,'Simple Correlation Matrix');
+
+%  loop through the file to find the pcm
+
+[pcm]=corrmat_read(fidi,'Partial Correlation Matrix');
+
+%  loop through the file to find the srcm
+
+[srcm]=corrmat_read(fidi,'Simple Rank Correlation Matrix');
+
+%  loop through the file to find the prcm
+
+[prcm]=corrmat_read(fidi,'Partial Rank Correlation Matrix');
+
+end
+
+%%  function to find and read the number of samples
+
+function [nsamp]=nsamp_read(fidi)
 
 [fline]=findline(fidi,'Statistics based on ');
@@ -186,7 +224,11 @@
 [ntokens,tokens]=fltokens(fline);
 nsamp=tokens{1}{4};
-display(sprintf('Dakota samples=%d.',nsamp));
-
-%%  loop through the file to find the moments
+display(sprintf('  Dakota samples=%d.',nsamp));
+
+end
+
+%%  function to find and read the moments
+
+function [dresp]=moments_read(fidi,dresp)
 
 [fline]=findline(fidi,'Moments for each response function');
@@ -195,77 +237,88 @@
 end
 
-display('Reading moments for each response function:');
-ndresp=0;
-
-while ~ieof
+display('Reading moments for response functions:');
+
+while 1
+    fline=fgetl(fidi);
+    if isempty(fline)
+        break;
+    end
+    [ntokens,tokens]=fltokens(fline);
+    
+%  add new response function and moments
+
+    dresp(end+1).descriptor=tokens{1}{ 1};
+    display(sprintf('  %s',dresp(end).descriptor));
+    dresp(end  ).mean      =tokens{1}{ 4};
+    dresp(end  ).stddev    =tokens{1}{ 8};
+    dresp(end  ).coefvar   =tokens{1}{13};
+end
+
+display(sprintf('  Number of Dakota response functions=%d.',...
+    length(dresp)));
+
+end
+
+%%  function to find and read the confidence intervals
+
+function [dresp]=cis_read(fidi,dresp)
+
+[fline]=findline(fidi,...
+    '95% confidence intervals for each response function');
+if ~ischar(fline)
+    return
+end
+
+display('Reading 95% confidence intervals for response functions:');
+
+while 1
+    fline=fgetl(fidi);
+    if isempty(fline)
+        break;
+    end
+    [ntokens,tokens]=fltokens(fline);
+   
+%  find response function associated with confidence intervals
+
+    idresp=0;
+    for i=1:length(dresp)
+        if strcmpi(tokens{1}{ 1},dresp(i).descriptor)
+            idresp=i;
+            break;
+        end
+    end
+    if ~idresp
+        idresp=length(dresp)+1;
+        dresp(idresp).descriptor=tokens{1}{ 1};
+        display(sprintf('  %s',dresp(idresp).descriptor));
+    end
+
+%  add confidence intervals to response functions
+
+    dresp(i).meanci  (1,1)=tokens{1}{ 5};
+    dresp(i).meanci  (2,1)=tokens{1}{ 6};
+    dresp(i).stddevci(1,1)=tokens{1}{12};
+    dresp(i).stddevci(2,1)=tokens{1}{13};
+end
+
+display(sprintf('  Number of Dakota response functions=%d.',...
+    length(dresp)));
+
+end
+
+%%  function to find and read the cdf's
+
+function [dresp]=cdfs_read(fidi,dresp)
+
+[fline]=findline(fidi,'Probabilities for each response function');
+if ~ischar(fline)
+    return
+end
+
+display('Reading CDF''s for response functions:');
+
+while ~isempty(fline)
     fline=fgetl(fidi);
     if ~ischar(fline)
-        ieof=1;
-        break;
-    end
-    if isempty(fline)
-        break;
-    end
-    
-%  add new response function and moments
-
-    ndresp=ndresp+1;
-    [ntokens,tokens]=fltokens(fline);
-    dresp(ndresp).descriptor=tokens{1}{ 1};
-    display(sprintf('    %s',dresp(ndresp).descriptor));
-    dresp(ndresp).mean      =tokens{1}{ 4};
-    dresp(ndresp).stddev    =tokens{1}{ 8};
-    dresp(ndresp).coefvar   =tokens{1}{13};
-end
-
-display(sprintf('Number of Dakota response functions=%d.',ndresp));
-
-%%  loop through the file to find the confidence intervals
-
-[fline]=findline(fidi,...
-    '95% confidence intervals for each response function');
-if ~ischar(fline)
-    return
-end
-
-display('Reading 95% confidence intervals for each response function.');
-
-while ~ieof
-    fline=fgetl(fidi);
-    if ~ischar(fline)
-        ieof=1;
-        break;
-    end
-    if isempty(fline)
-        break;
-    end
-    
-%  add confidence intervals to response functions
-
-    [ntokens,tokens]=fltokens(fline);
-    for i=1:ndresp
-        if strcmpi(tokens{1}{ 1},dresp(i).descriptor)
-            dresp(i).meanci  (1,1)=tokens{1}{ 5};
-            dresp(i).meanci  (2,1)=tokens{1}{ 6};
-            dresp(i).stddevci(1,1)=tokens{1}{12};
-            dresp(i).stddevci(2,1)=tokens{1}{13};
-            break;
-        end
-    end
-end
-
-%%  loop through the file to find the probabilities
-
-[fline]=findline(fidi,'Probabilities for each response function');
-if ~ischar(fline)
-    return
-end
-
-display('Reading CDF''s for each response function.');
-
-while ~ieof && ~isempty(fline)
-    fline=fgetl(fidi);
-    if ~ischar(fline)
-        ieof=1;
         break;
     end
@@ -279,5 +332,5 @@
 
         idresp=0;
-        for i=1:ndresp
+        for i=1:length(dresp)
             if strcmpi(tokens{1}{ 6},dresp(i).descriptor)
                 idresp=i;
@@ -285,5 +338,10 @@
             end
         end
-    
+        if ~idresp
+            idresp=length(dresp)+1;
+            dresp(idresp).descriptor=tokens{1}{ 6};
+            display(sprintf('  %s',dresp(idresp).descriptor));
+        end
+
 %  skip column headings of cdf
 
@@ -296,5 +354,5 @@
         icdf=0;
         while ~isempty(fline) && ...
-                ~strncmpi(fline,'Cumulative Distribution Function',32)
+              ~strncmpi(fline,'Cumulative Distribution Function',32)
             [ntokens,tokens]=fltokens(fline);
             icdf=icdf+1;
@@ -313,61 +371,26 @@
 end
 
-%%  loop through the file to find the scm
-
-[fline]=findline(fidi,'Simple Correlation Matrix');
-if ~ischar(fline)
-    scm=[];
-    return
-end
-
-display('Reading simple correlation matrix.');
-scm=corrmat(fidi,fline);
-
-%%  loop through the file to find the pcm
-
-[fline]=findline(fidi,'Partial Correlation Matrix');
-if ~ischar(fline)
-    pcm=[];
-    return
-end
-
-display('Reading partial correlation matrix.');
-pcm=corrmat(fidi,fline);
-
-%%  loop through the file to find the srcm
-
-[fline]=findline(fidi,'Simple Rank Correlation Matrix');
-if ~ischar(fline)
-    srcm=[];
-    return
-end
-
-display('Reading simple rank correlation matrix.');
-srcm=corrmat(fidi,fline);
-
-%%  loop through the file to find the prcm
-
-[fline]=findline(fidi,'Partial Rank Correlation Matrix');
-if ~ischar(fline)
-    prcm=[];
-    return
-end
-
-display('Reading partial rank correlation matrix.');
-prcm=corrmat(fidi,fline);
-
-end
-
-%%  function to read a correlation matrix at current file position
-
-function [cmat]=corrmat(fidi,fline)
-
-ieof=0;
+display(sprintf('  Number of Dakota response functions=%d.',...
+    length(dresp)));
+
+end
+
+%%  function to find and read a correlation matrix
+
+function [cmat]=corrmat_read(fidi,cmstr)
+
+[fline]=findline(fidi,cmstr);
+if ~ischar(fline)
+    cmat=struct([]);
+    return
+end
+
+display(['Reading ''' fline '''.']);
+
 cmat.title=fline;
 
-while ~ieof && ~isempty(fline)
+while ~isempty(fline)
     fline=fgetl(fidi);
     if ~ischar(fline)
-        ieof=1;
         break;
     end
@@ -384,8 +407,8 @@
     end
     
-%  process rows of matrix
+%  process rows of matrix, reading until blank line
 
     nrow=0;
-    while ~ieof
+    while 1
         fline=fgetl(fidi);
         if isempty(fline)
@@ -413,19 +436,35 @@
 function [dresp]=nond_locrel(fidi)
 
+%  loop through the file to find the number of function evaluations
+
+[nfeval]=nfeval_read(fidi);
+
+%  loop through the file to find the statistics
+
+[dresp]=mvstats_read(fidi,struct([]));
+
+end
+
+%%  function to find and read the number of function evaluations
+
+function [nfeval]=nfeval_read(fidi)
+
+[fline]=findline(fidi,'<<<<< Function evaluation summary');
+if ~ischar(fline)
+    return
+end
+
+[ntokens,tokens]=fltokens(fline);
+nfeval=tokens{1}{5};
+display(sprintf('  Dakota function evaluations=%d.',nfeval));
+
+end
+
+%%  function to find and read the MV statistics
+
+function [dresp]=mvstats_read(fidi,dresp)
+
 ieof=0;
 
-%%  loop through the file to find the Dakota statistics
-
-[fline]=findline(fidi,'<<<<< Function evaluation summary');
-if ~ischar(fline)
-    return
-end
-
-[ntokens,tokens]=fltokens(fline);
-nfunc=tokens{1}{5};
-display(sprintf('Dakota function evaluations=%d.',nfunc));
-
-%%  loop through the file to find the statistics
-
 [fline]=findline(fidi,'MV Statistics for ');
 if ~ischar(fline)
@@ -433,5 +472,5 @@
 end
 
-display('Reading MV statistics for each response function:');
+display('Reading MV statistics for response functions:');
 ndresp=0;
 
@@ -441,14 +480,13 @@
 %  add new response function and moments
 
-    ndresp=ndresp+1;
     [ntokens,tokens]=fltokens(fline);
-    dresp(ndresp).descriptor=tokens{1}{4};
-    display(sprintf('    %s',dresp(ndresp).descriptor));
+    dresp(end+1).descriptor=tokens{1}{4};
+    display(sprintf('  %s',dresp(end).descriptor));
     fline=fgetl(fidi);
     [ntokens,tokens]=fltokens(fline);
-    dresp(ndresp).mean      =tokens{1}{5};
+    dresp(end  ).mean      =tokens{1}{5};
     fline=fgetl(fidi);
     [ntokens,tokens]=fltokens(fline);
-    dresp(ndresp).stddev    =tokens{1}{7};
+    dresp(end  ).stddev    =tokens{1}{7};
 
 %  read and add importance factors to response function
@@ -465,6 +503,6 @@
         [ntokens,tokens]=fltokens(fline);
         idvar=idvar+1;
-        dresp(ndresp).desvar(idvar,1)=cellstr(tokens{1}{5});
-        dresp(ndresp).impfac(idvar,1)=        tokens{1}{7};
+        dresp(end).desvar(idvar,1)=cellstr(tokens{1}{5});
+        dresp(end).impfac(idvar,1)=        tokens{1}{7};
 
         fline=fgetl(fidi);
@@ -478,7 +516,7 @@
 
     if ~idvar
-        display('      Importance Factors not available.');
-        dresp(ndresp).desvar={};
-        dresp(ndresp).impfac=[];
+        display('    Importance Factors not available.');
+        dresp(end).desvar={};
+        dresp(end).impfac=[];
         while ~ieof && ...
                 ~strncmpi(fline,'Cumulative Distribution Function',32) && ...
@@ -504,9 +542,14 @@
 
         idresp=0;
-        for i=1:ndresp
-            if strcmpi(tokens{1}{6},dresp(i).descriptor)
+        for i=1:length(dresp)
+            if strcmpi(tokens{1}{ 6},dresp(i).descriptor)
                 idresp=i;
                 break;
             end
+        end
+        if ~idresp
+            idresp=length(dresp)+1;
+            dresp(idresp).descriptor=tokens{1}{ 6};
+            display(sprintf('  %s',dresp(idresp).descriptor));
         end
     
@@ -540,5 +583,5 @@
 
     if ~icdf
-        display('      Cumulative Distribution Function not available.');
+        display('    Cumulative Distribution Function not available.');
         dresp(ndresp).cdf=[];
         while ~ieof && ...
@@ -555,5 +598,6 @@
 end
 
-display(sprintf('Number of Dakota response functions=%d.',ndresp));
+display(sprintf('  Number of Dakota response functions=%d.',...
+    length(dresp)));
 
 end
@@ -571,15 +615,17 @@
 display(['  ' deblank(fline)]);
 
-[fline]=findline(fidi,'<<<<< Function evaluation summary');
-if ~ischar(fline)
-    return
-end
-display(['  ' deblank(fline)]);
-
-[ntokens,tokens]=fltokens(fline);
-nfunc=tokens{1}{5};
-% display(sprintf('Dakota function evaluations=%d.',nfunc));
-
-%%  loop through the file to find the best evaluation
+%  loop through the file to find the number of function evaluations
+
+[nfeval]=nfeval_read(fidi);
+
+%  loop through the file to find the best evaluation
+
+[dresp]=best_read(fidi,struct([]));
+
+end
+
+%%  function to find and read the best evaluation
+
+function [dresp]=best_read(fidi,dresp)
 
 [fline]=findline(fidi,'<<<<< Best ');
@@ -587,5 +633,5 @@
     return
 end
-dresp.best=[];
+dresp(end+1).best=[];
 
 display('Reading values for best function evaluation:');
