% -------------------------------------------------------
% MEAM 247 Instron Data Extraction
% Author: J. Fiene
% Version: 2.0
% Date: Oct 26, 2009
%
% -------------------------------------------------------
%
% time, crosshead, load, extensometer, gauge_diameter, gauge_length = extract(FILENAME)
%
% -------------------------------------------------------
%
% where:
%  FILENAME is a text string of the file (including path, if appropriate)
%
% and the result is a six-element struct containing:
%  TIME is the time in seconds
%  CROSSHEAD is the crosshead displacement in inches
%  LOAD is the load in pounds
%  EXTENSOMETER is the extensometer displacement in inches
%  GAUGE_LENGTH is the original specimen length in inches
%  GAUGE_DIAMETER is the original specimen diameter in inches
%
% -------------------------------------------------------
%
% EXAMPLE:
%
% calling
%   result = extract('file.xls')
%
% will place the struct "result" in the workspace, which can be accessed
% as:
%   result.time
%   result.crosshead
%   result.load
%   result.extensometer
%   result.gauge_length
%   result.gauge_diameter
% -------------------------------------------------------

function output = extract(filename)

% extract gauge dimensions:
[text,values] = textread(filename,'%s %s',6,'delimiter','\t');
gauge_diameter = str2num(values{4});
gauge_length = str2num(values{5});

disp(['Extracting data from "' filename '" created on ' values{1} ' @ ' values{2}])

%extract scale factors
[crosshead_scale_factor,load_scale_factor,extensometer_scale_factor] = textread(filename,'%n %n %n',1,'headerlines',13);

[time,crosshead_raw,load_raw,extensometer_raw] = textread(filename,'%f %f %f %f','headerlines',14);

crosshead = crosshead_raw * crosshead_scale_factor;
load = load_raw * load_scale_factor;
extensometer = extensometer_raw * extensometer_scale_factor;

output = struct('time',time,'crosshead',crosshead,'load',load,'extensometer',extensometer,'gauge_diameter',gauge_diameter,'gauge_length',gauge_length);