Project

General

Profile


<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!--
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
--><title>testFit</title><meta name="generator" content="MATLAB 8.4"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2014-09-23"><meta name="DC.source" content="testFit.m"><style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}

html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }

h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }

a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }

p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }

ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }

.content { font-size:1.2em; line-height:140%; padding: 20px; }

pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }

@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }

span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }

.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }

table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }





</style></head><body><div class="content"><h2>Contents</h2><div><ul><li><a href="#2">Example 2</a></li><li><a href="#3">Defaults</a></li><li><a href="#4">Finding Names of all Text Files in the ModelResults Directory</a></li><li><a href="#5">Reading, Averaging, and Fitting all Text Files</a></li><li><a href="#7">Read in the Relevant Model Data</a></li><li><a href="#8">Filter, Fit and Evaluate Spline on Model Data</a></li><li><a href="#9">Plot the Results</a></li><li><a href="#10">Output Spline Results to an Excel File</a></li></ul></div><pre class="codeinput"><span class="keyword">function</span> testFit(desiredTest,splineSample)
</pre><h2>Example 2<a name="2"></a></h2><p>Input values desiredTest = 60, splineSample = 200;</p><p>Load in output of a desired model, fit, filter and evaluate a spline Output written into an excel data file</p><h2>Defaults<a name="3"></a></h2><pre class="codeinput"><span class="keyword">if</span> ~nargin
desiredTest = 60;
splineSample = 200;
<span class="keyword">end</span>
</pre><h2>Finding Names of all Text Files in the ModelResults Directory<a name="4"></a></h2><pre class="codeinput">filexlsName = <span class="string">'FinalResults.xls'</span>;
dirName = fullfile(pwd, <span class="string">'ModelResults'</span>);
modelFiles = dir(fullfile(dirName, [<span class="string">'*.'</span>, <span class="string">'txt'</span>]));

<span class="keyword">if</span> isempty(modelFiles)
error(<span class="string">'testFitEx:NoDataFiles'</span>, <span class="keyword">...</span>
<span class="string">'No data files.\nRun "FileGenerator.m" in the ModelResults folder'</span>);
<span class="keyword">else</span>
numModels = length(modelFiles);
<span class="keyword">end</span>

disp(<span class="string">'Processing test results...'</span>);
</pre><pre class="codeoutput">Processing test results...
</pre><h2>Reading, Averaging, and Fitting all Text Files<a name="5"></a></h2><pre class="codeinput"><span class="keyword">for</span> id = 1:numModels
</pre><h2>Read in the Relevant Model Data<a name="7"></a></h2><pre class="codeinput"> fileName = fullfile(dirName, modelFiles(id).name);
[tmp, modelName] = fileparts(modelFiles(id).name);
fid = fopen(fileName);

<span class="comment">% Getting the number of timesteps from the header of the txt file</span>

nTimes = textscan(fid, <span class="string">'%*s %d \n'</span>, 1);
nTimes = nTimes{1};

<span class="comment">% Read through models we don't want data from and keep resaving model</span>
<span class="comment">% info into modelData until it gets to desiredModel</span>

<span class="keyword">for</span> j = 1:desiredTest-1
fgets(fid); <span class="comment">% read test header line</span>
textscan(fid, <span class="string">'%*f %*f \n'</span>, nTimes); <span class="comment">% get model output data</span>
<span class="keyword">end</span>

fgets(fid); <span class="comment">% read test header line</span>
modelData = textscan(fid, <span class="string">'%f %f \n'</span>, nTimes); <span class="comment">% get model output data</span>
modelData = cell2mat(modelData);

fclose(fid);
</pre><h2>Filter, Fit and Evaluate Spline on Model Data<a name="8"></a></h2><pre class="codeinput"> nPoints = 10; <span class="comment">% setting number of points to average over</span>
b = (1 / nPoints) * ones(1, nPoints); <span class="comment">% moving average over nPoints</span>
filterData = filter(b, 1, modelData); <span class="comment">% create moving average</span>

splineData = spline(filterData(:, 1), filterData(:, 2)); <span class="comment">% create spline</span>
splineTime = linspace(0, filterData(end, 1), splineSample);
finalData = ppval(splineData, splineTime); <span class="comment">% evaluate spline</span>
</pre><h2>Plot the Results<a name="9"></a></h2><pre class="codeinput"> figure;

subplot(2, 1, 1)
plot(modelData(:, 1), modelData(:, 2));
line(filterData(:, 1), filterData(:, 2), <span class="string">'Color'</span>, <span class="string">'r'</span>);
title(<span class="string">'Original and Moving Average'</span>)

subplot(2, 1, 2)
plot(filterData(:, 1), filterData(:, 2));
line(splineTime, finalData, <span class="string">'Color'</span>, <span class="string">'r'</span>, <span class="string">'Marker'</span>, <span class="string">'+'</span>);
title(<span class="string">'Moving Average and Spline'</span>)


<span class="comment">%saveas(gcf, fullfile('PlotFigs', [modelName, '.fig']));</span>
</pre><img vspace="5" hspace="5" src="testFit_01.png" alt=""> <img vspace="5" hspace="5" src="testFit_02.png" alt=""> <img vspace="5" hspace="5" src="testFit_03.png" alt=""> <img vspace="5" hspace="5" src="testFit_04.png" alt=""> <img vspace="5" hspace="5" src="testFit_05.png" alt=""> <img vspace="5" hspace="5" src="testFit_06.png" alt=""> <img vspace="5" hspace="5" src="testFit_07.png" alt=""> <img vspace="5" hspace="5" src="testFit_08.png" alt=""> <img vspace="5" hspace="5" src="testFit_09.png" alt=""> <img vspace="5" hspace="5" src="testFit_10.png" alt=""> <h2>Output Spline Results to an Excel File<a name="10"></a></h2><pre class="codeinput"> xlsPlace = sprintf(<span class="string">'%c1:%c%d'</span>, <span class="string">'A'</span>+2*id-2, <span class="string">'A'</span>+2*id-1, splineSample);
xlswrite(filexlsName, <span class="keyword">...</span>
[{sprintf(<span class="string">'time%03d'</span>, id), sprintf(<span class="string">'data%03d'</span>, id)}; <span class="keyword">...</span>
num2cell([splineTime; finalData]')], xlsPlace);
</pre><pre class="codeinput"><span class="keyword">end</span>

close <span class="string">all</span> <span class="comment">% close all figures</span>
</pre><p class="footer">Copyright 2007-2013 The MathWorks, Inc.<br><a href="http://www.mathworks.com/products/matlab/">Published with MATLAB&reg; R2015a</a><br></p></div><!--
##### SOURCE BEGIN #####
% Copyright 2007-2013 The MathWorks, Inc.

function testFit(desiredTest,splineSample)
%% Example 2
% Input values desiredTest = 60, splineSample = 200;
%
% Load in output of a desired model, fit, filter and evaluate a spline
% Output written into an excel data file

%% Defaults

if ~nargin
desiredTest = 60;
splineSample = 200;
end

%% Finding Names of all Text Files in the ModelResults Directory

filexlsName = 'FinalResults.xls';
dirName = fullfile(pwd, 'ModelResults');
modelFiles = dir(fullfile(dirName, ['*.', 'txt']));

if isempty(modelFiles)
error('testFitEx:NoDataFiles', ...
'No data files.\nRun "FileGenerator.m" in the ModelResults folder');
else
numModels = length(modelFiles);
end

disp('Processing test results...');

%% Reading, Averaging, and Fitting all Text Files
for id = 1:numModels
%% Read in the Relevant Model Data
fileName = fullfile(dirName, modelFiles(id).name);
[tmp, modelName] = fileparts(modelFiles(id).name);
fid = fopen(fileName);
% Getting the number of timesteps from the header of the txt file
nTimes = textscan(fid, '%*s %d \n', 1);
nTimes = nTimes{1};
% Read through models we don't want data from and keep resaving model
% info into modelData until it gets to desiredModel
for j = 1:desiredTest-1
fgets(fid); % read test header line
textscan(fid, '%*f %*f \n', nTimes); % get model output data
end
fgets(fid); % read test header line
modelData = textscan(fid, '%f %f \n', nTimes); % get model output data
modelData = cell2mat(modelData);
fclose(fid);
%% Filter, Fit and Evaluate Spline on Model Data
nPoints = 10; % setting number of points to average over
b = (1 / nPoints) * ones(1, nPoints); % moving average over nPoints
filterData = filter(b, 1, modelData); % create moving average
splineData = spline(filterData(:, 1), filterData(:, 2)); % create spline
splineTime = linspace(0, filterData(end, 1), splineSample);
finalData = ppval(splineData, splineTime); % evaluate spline

%% Plot the Results
figure;
subplot(2, 1, 1)
plot(modelData(:, 1), modelData(:, 2));
line(filterData(:, 1), filterData(:, 2), 'Color', 'r');
title('Original and Moving Average')
subplot(2, 1, 2)
plot(filterData(:, 1), filterData(:, 2));
line(splineTime, finalData, 'Color', 'r', 'Marker', '+');
title('Moving Average and Spline')

%saveas(gcf, fullfile('PlotFigs', [modelName, '.fig']));

%% Output Spline Results to an Excel File

xlsPlace = sprintf('%c1:%c%d', 'A'+2*id-2, 'A'+2*id-1, splineSample);
xlswrite(filexlsName, ...
[{sprintf('time%03d', id), sprintf('data%03d', id)}; ...
num2cell([splineTime; finalData]')], xlsPlace);

end

close all % close all figures

##### SOURCE END #####
--></body></html>
(1-1/12)