Excel Data Manipulation
%% BMES 202: code 2_1
%% CODE
function [data, c_head, csv_file]= ca02_fun1(csv_file)
% If user does not provide an input file name (nargin), prompt the user to select a csv file [uigetfile]
...and create a variable to hold the full file name (file name as a function output (csv_file [1xN] character array)).
if nargin == 0
[filename, path]=uigetfile('*.csv','Choose file','AG_1p_test1.csv');
csv_file=fullfile(path, filename);
end
% Read in the data [csv_read] (data Mx3 numeric array) and the header information
...(c_head [2x1] cell array) [fopen, fgetl, fclose, !! can't use uiimport], and provide these as function outputs.
data=csvread(csv_file,2,0);
fileID=fopen(csv_file,'rt');
fgetl(fileID);
c_head(1,1)={fgetl(fileID)};
c_head(2,1)={fgetl(fileID)};
fclose(fileID);
% In figure1, visualize the raw data (time vs load, time vs displacement) [plot, subplot]
figure(1);
subplot(2,1,1);
plot(data(:,1),data(:,2),'-')
xlabel('Time (sec)'), ylabel('Force (N)'), title('Time (sec) vs Force (N)')
subplot(2,1,2);
plot(data(:,1),data(:,3),'-')
xlabel('Time (sec)'), ylabel('Displacement (mm)'), title('Time (sec) vs Displacement (mm)')
return;
%% BMES 202: code 2_2
%% CODE
function [dt_hold] = ca02_fun2(data)
% Create a new figure (figure 2), and plot time versus load (make sure load is positive)
figure(2)
plot(data(:,1),data(:,2),'-');
xlabel('Time (sec)'), ylabel('Load (N)');
title('Time (sec) vs Load (N)');
% Prompt the user (msgbox, uiwait) to select the beginning of the hold [ginput]
uiwait(msgbox('Please select the beginning of the hold'));
[x y]=ginput(1);
% Find the rows of the hold data based on the point the user selected [find]
rows=find(data(:,1)> x & -data(:,2)> y);
% Create an output variable which holds the data in only the rows from step 8 (dt_hold)
dt_hold=data(rows,:);
% Overlay the 'hold' data to figure 2.
figure(2)
hold on
plot(dt_hold(:,1),-dt_hold(:,2)),'r-')
return;
%% BMES 202: code 2_3
%% CODE
function [fit_parms, d_err] = ca02_fun3(dt_hold)
%(Analysis Function)
% In a new figure (figure 3), plot the time vs displacement data (be sure to 'shift' the time data so that
...it starts at 0, and displacement is positive)
figure(3);
dt_new=dt_hold-dt_hold(1);
plot(dt_new(:,1),-dt_new(:,3),'-');
xlabel('TIme (sec)'), ylabel('Displacement (mm)')
% Set the upper and lower limits on the parameters for your equation
ul=dt_new(1); %upper limit
ll=dt_new(end,1); %lower limit
% Set the initial guess for the parameters a, b, c
a=-dt_new(1,3); %at t=0 and y=a
b=-dt_new(end,3)-(-dt_new(1,3)); %scaling factor: last-first value
max_tc=a+0.632*b;
%The time that fits this description
possible_values=find(-dt_new(:,3)>max_tc*(1-1e-4) & -dt_new(:,3)<max_tc*(1+1e-4));
%therefore
c=dt_new(possible_values(1),1);
% Fit the equation to the 'hold' displacement/time data [lsqcurvefit] and provide the
...best-fit parameters as outputs of your function.
fit_parms=lsqcurvefit(@creep_fun1 ,[a b c], dt_new(:,1), -dt_new(:,3), [0 0 0], 1.5*[a b c]);
y_data= fit_parms(1)+fit_parms(2)*(1-exp(-dt_new(:,1)/fit_parms(3)));%from the equation given
%plot
hold on;
plot(dt_new(:,1),-y_data,'ro')
% Calculate the estimated displacement from the best-fit parameters, and overlay that data onto figure 3.
% Calculate the error of the fit (difference in estimate and actual displacement) [diff]
d_err=diff([y_data -dt_new(:,3)],2);
return
%% BMES 202: code 2_4
%% CODE
function ca02_fun4()
% Call functions 1-3 in sequence
[data, c_head, csv_file]= ca02_fun1();
[dt_hold] = ca02_fun2(data);
[fit_parms, d_err] = ca02_fun3(dt_hold);
% Calculate the root-mean-square error (RMSE)
rmse = rms(d_err)
% Take the output from functions 1-3, and format the following parameters
% to screen for copy & paste (sprintf, disp): 'Filename, a(mm), b (mm), c
% (sec), RMSE' (RMSE is root-mean-square-error)
[pth name ext] = fileparts(csv_file);
out(1, 1) = {sprintf('%%Filename\ta(mm)\tb(mm)\tc(sec)\tRMSE')};
% The first line of the output file sets up the headers for the variables
% that are being reported in addition to their units. They are formatted
% using sprintf.
out(2, 1) = {sprintf('%s\t%.2f\t%.2f\t%.2f\t%.4f', [name ext], ...
fit_parms(1), fit_parms(2), fit_parms(3), rmse)};
% The second line of the output file prints the filename and other
% variables with the desired number of significant figures.
char(out) % The output is converted to characters and ready for
% copy-and-paste to Excel.
return
%%MHOMO KADIRI (c)2014










