function [dma] = readsDMA(file)
% clc
% file = './resultsFreqstrain/LCE_strain_001Hz.txt';
clear dma dimension


%% Declarations
i=1;                %nb of measurements in 1 cycle
dma = struct;       % Final structure with all data
dimension = struct;
units = {};         % Cell array with read units

%% Open file
fid = fopen(file);

%% Reads headings (-> until sees word start)
line = fgetl(fid);
while isempty(strfind(lower(line(2:2:end)),'start')) && ~feof(fid)
    line = fgetl(fid); % blank line
    line = fgetl(fid);
    % Finds sample mass
    if strfind(lower(line(2:2:end)),'size')
        if isempty(strfind(lower(line(2:2:end)),'mm'))
            error('length units are not mm for sample dimension');
        else
            dimension.units = 'mm';
            beg =2*(strfind(lower(line(2:2:end)),'size')+size('size',2)+1);
            fin =2*(strfind(lower(line(2:2:end)),'mm'));
            values(1,:) = str2num(line(beg:2:fin(1)-1));
            dimension.length = values(1,1);
            dimension.width = values(1,2);
            dimension.thickness = values(1,3);
            dimension.section = dimension.width*dimension.thickness;
            dimension.volume = dimension.section*dimension.length;
        end
    end
    % finds nb of signals = nb of columns and reads column title
    if strfind(lower(line(2:2:end)),'nsig')
        beg = 2*(strfind(lower(line(2:2:end)),'nsig')+size('nsig',2)+1);
        nbsig = str2num(line(beg:2:end));
        for m = 1:nbsig
            line = fgetl(fid); % blank line
            line = fgetl(fid);
            if m<=9
                if ~isempty(strfind(lower(line(2:2:end)),'('))
                    beg =2*(strfind(lower(line(2:2:end)),'sig')+size('sig',2)+2);
                    fin =2*(strfind(lower(line(2:2:end)),'(')-1);
                    string = strrep(lower(line(beg:2:fin)),' ','');
                else
                    beg =2*(strfind(lower(line(2:2:end)),'sig')+size('sig',2)+2);
                    %                 fin =2*(strfind(lower(line(2:2:end)),'(')-1);
                    string = strrep(lower(line(beg:2:end)),' ','');
                end
            else
                beg =2*(strfind(lower(line(2:2:end)),'sig')+size('sig',2)+3);
                fin =2*(strfind(lower(line(2:2:end)),'(')-1);
                string = strrep(lower(line(beg:2:fin)),' ','');
            end
            % Puts column title as field in substructure dma
            dma = setfield(dma,string,[]);
            % Reads units
            if ~isempty(strfind(lower(line(2:2:end)),'('))
                beg =2*(strfind(lower(line(2:2:end)),'(')+1);
                fin =2*(strfind(lower(line(2:2:end)),')')-1);
                string = lower(line(beg:2:fin));
                units(m) = {string};
            else
                units(m)={'none'};
            end
        end
    end
end


%% Reads data
while ~feof(fid)
    line = fgetl(fid);
    line = fgetl(fid);
    if ~isempty(str2num(line(2:2:end)))&&str2num(line(2:2:6))~=-1
               A(i,1:nbsig) = str2num(line(2:2:end));
    end
        i=i+1;

end
%% Closes file
fclose(fid);



%% Puts data in final structure dma
dma = setfield(dma,'dimension',[]);
dma.dimension = dimension;
namec = fieldnames(dma);
namem = fieldnames(dimension);

for j= 1:nbsig
    dma.(char(namec(j))) = A(:,j);
end



%% Print created structures
 fprintf('the DMA structure contains the fields:\n')
 for i=1:nbsig+1
 fprintf('\n %s \n',char(namec(i)));
 end
 fprintf('where %s is a substructure.\n',char(namec(end)));
 fprintf('The structure  %s contains: \n %s \n %s \n %s \n %s. \n',...
     char(namec(end)),char(namem(1)),char(namem(2)),char(namem(3)),char(namem(4)));
