function [data] = readMTS(file)

clear data
% file = 'LCE1Coupe_tractiontot.txt';

fid = fopen(file);
while ~feof(fid)
    line = fgetl(fid);
    %% find the specimens length and units for the length
    while isempty(strfind(lower(line(1:1:end)),'beginspecimen'))&&~feof(fid)
        line = fgetl(fid);
        if ~isempty(strfind(lower(line),'gagelength'))
            comma = strfind(line,',');
            beg = comma(1)+2;
            fin = comma(2)-1;
            gagelength = str2num(line(beg:fin));
            quotes = strfind(line,'"');
            beg = quotes(1)+1;
            fin = quotes(2)-1;
            recunits{3} = line(beg:fin);
            circunits{2} = line(beg:fin);
        end
    end
    %% find the specimens other dimensions and units
    % depends on whether the cross section is rectangular
    % or circular
    if ~isempty(strfind(lower(line(1:1:end)),'beginspecimen'))
        while isempty(strfind(lower(line(1:1:end)),'begindata'))&&~feof(fid)
            line = fgetl(fid);
            if ~isempty(strfind(lower(line),'width'))
                crossection = 'rectangular';
                comma = strfind(line,',');
                beg = comma(1)+2;
                fin = comma(2)-1;
                width = str2num(line(beg:fin));
                quotes = strfind(line,'"');
                beg = quotes(1)+1;
                fin = quotes(2)-1;
                recunits{1} = line(beg:fin);
            end
            if ~isempty(strfind(lower(line),'thickness'))
                comma = strfind(line,',');
                beg = comma(1)+2;
                fin = comma(2)-1;
                thickness = str2num(line(beg:fin));
                quotes = strfind(line,'"');
                beg = quotes(1)+1;
                fin = quotes(2)-1;
                recunits{2} = line(beg:fin);
            end
            if ~isempty(strfind(lower(line),'diameter'))
                crossection = 'circular';
                comma = strfind(line,',');
                beg = comma(1)+2;
                fin = comma(2)-1;
                diameter = str2num(line(beg:fin));
                quotes = strfind(line,'"');
                beg = quotes(1)+1;
                fin = quotes(2)-1;
                circunits{1} = line(beg:fin);
            end
        end
    end
    %% find the number of signals, label the signals
    % in cell array parameter
    if ~isempty(strfind(lower(line(1:1:end)),'begindata'))
        line = fgetl(fid);
        commas = strfind(line,',');
        nbsig = length(commas)+1;
        for i=1:nbsig
            if i==1
                % find first non-space character index
                %                 beg(i) = 4;
                for j=1:length(line)
                    if ~strcmp(line(1),line(j))
                        beg(i) = j;
                        break
                    end
                end
                fin(i) = commas(i)-1;
            elseif i==nbsig
                % find last non-space character index
                fin(i) = length(line);
                beg(i) = commas(i-1)+2;
            else
                beg(i)= commas(i-1)+2;
                fin(i) = commas(i)-1;
            end
            if strcmp('_',line(beg(i)))
                beg(i) = beg(i)+1;
            end
            parameter{i} = lower(line(beg(i):fin(i)));
        end
        %% this part just gets the units of the signals or
        % parameters
        line=fgetl(fid);
        quotes = strfind(line,'"');
        for i=1:nbsig
            beg(i)=quotes(2*i-1);
            fin(i)=quotes(2*i);
            if (fin(i)-beg(i))==1
                units{i} = 'N/a';
            else
                units{i}=line(beg(i)+1:fin(i)-1);
            end
        end
        
    end
    
end
fclose(fid);

%% makes data structure and reads all of the data in the txt
% adds the data values to data.whatevertheparameteris
data = struct;
fid = fopen(file);
line = fgetl(fid);
A=[];
k=1;
while isempty(strfind(lower(line(1:1:end)),'begindata')) && ~feof(fid)
    line = fgetl(fid);
end
while isempty(strfind(lower(line(1:1:end)),'enddata')) && ~feof(fid)
    if ~isempty(str2num(line))
        A(k,1:nbsig) = str2num(line);
        k=k+1;
    end
    line = fgetl(fid);
end
% find things below begindata
for j=1:nbsig
    data = setfield(data,parameter{j},A(:,j));
end


%% find dimensions from above and give dimensions and units
% add the dimensions and units to the data structure
% and calculated area
dimensions = struct;

if strcmp(crossection,'rectangular')
    dimensions.width = width;
    dimensions.thickness = thickness;
    dimensions.gagelength = gagelength;
    % area is t*w
    dimensions.area = dimensions.width*dimensions.thickness;
    dimensions.units = recunits;
elseif strcmp(crossection,'circular')
    dimensions.diameter = diameter;
    dimensions.gagelength = gagelength;
    %area is pi*D^2/4
    dimensions.area = pi/4*dimensions.diameter^2;
    dimensions.units = circunits;
end
data.dimensions = dimensions;
data.units = units;

%% shifts strain values if they do not start at 0

% if data.strain(1)<0
%     data.strain = data.strain-data.strain(1);
% end
fclose(fid);

end