TA_txt2MAT

by

 

converts text datafile from TA rheometer to a .mat datafile.

TA_txt2MAT.m
function TA_txt2MAT
%converts TA rheology text files (exported as csv) into matlab binary files
%just change working directory to the one with the text files, and run
%TA2MAT without any arguments. For each of the text files in the directory
%it stores a parallel .mat binary version, by appending .mat.
%i.e. filename.txt => filename.txt.mat
%Program skips the .txt files that have already been converted.
%
%*******************************************
%sample TA txt file looks like this
% Filename	M15%Bnorm1000srpt1
% Instrument name	.....
% Operator	
% rundate	5/25/2013
% [step]
% Conditioning Step
% 
% 
% 
% [step]
% Time - 1
% Angular frequency	Storage modulus	Loss modulus	Gap	Oscillation strain	Oscillation strain rate	Oscillation stress	Oscillation torque	Temperature	Time	Step time	Axial force	Run time
% rad/s	Pa	Pa	mm	%	1/s	Pa	N.m	C	s	s	N	s
% 0.1	738.824	5142.45	3.499	100.126	0.100126	5201.78	65367.5	27.569	331.602	331.602	9.36379	337.116
% 
% [step]
% Time - 2
% Angular frequency	Storage modulus	Loss modulus	Gap	Oscillation strain	Oscillation strain rate	Oscillation stress	Oscillation torque	Temperature	Time	Step time	Axial force	Run time
% rad/s	Pa	Pa	mm	%	1/s	Pa	N.m	C	s	s	N	s
% 0.1259	1258.21	6930.63	3.5	79.39	0.099952	5592.17	70273.3	27.579	520.589	174.693	9.36398	526.104
% 
% [step]
% Time - 3
% Angular frequency	Storage modulus	Loss modulus	Gap	Oscillation strain	Oscillation strain rate	Oscillation stress	Oscillation torque	Temperature	Time	Step time	Axial force	Run time
% rad/s	Pa	Pa	mm	%	1/s	Pa	N.m	C	s	s	N	s
% 0.1585	2004.22	9195.72	3.499	63.066	0.0999596	5935.52	74587.9	27.588	684.597	138.759	9.25598	690.111
%
%*******************************************
%information saved into the .mat file is
%header fields, unitsline, and
% n x m matrix, where n is the number of rows, i.e. expts, and m is the
% number of columns, i.e. header fields
%
%plotting help:
%in your script include these inline commands
%col = @(str) strncmpi( header, str, numel(str ) );
%quick_plot = @(x_var, y_var) [plot( data( :, col(x_var) ), data( :, col(y_var) ) ); xlabel( header( col( x_var) ) ); ylabel( header( col( y_var) ) )];


%%
d = dir( '*.txt' );
for i = 1:numel(d );
    out_file = strcat( d(i).name, '.mat' );
    if ~exist( out_file, 'file' )
        fprintf( 'processing %s \n', d(i).name );
        fid = fopen( d(i).name, 'r' );
        data = [];
        str = {};
        while ~feof( fid )
            tmp = fgetl( fid );
            vals1 = sscanf( tmp, '%f,' );
            vals2 = sscanf( tmp, '%f ' );
            if numel( vals2 ) > numel( vals1 )
                vals = vals2;
            else
                vals = vals1;
            end
            if isempty( vals )
                str{end+1} = tmp;
            else
                if numel(str)>2;
                    headerline = str{end-1};
                    unitsline = str{end};
                end
                if size( data, 2 ) < numel(vals )
                    data = [data, nan( size( data, 1), numel( vals) - size( data, 2) )];
                end
                data(end+1, 1:numel(vals)) = vals';
            end
        end
        fclose( fid );
        %%
        a = regexp( headerline, ',' );
        b = regexp( headerline, '\t' );
        i = [0 union( a, b ), length( headerline ) + 1];
        for j=1: numel(i)-1;
            header{j} = headerline( i(j)+1:i(j+1)-1 );
        end
        a = regexp( unitsline, ',' );
        b = regexp( unitsline, '\t' );
        i = [0 union( a, b ), length( unitsline ) + 1];
        for j=1: numel(i)-1;
            units{j} = regexprep( unitsline( i(j)+1:i(j+1)-1 ), char(194), '' );
        end
        %%
        save( out_file, 'data', 'header', 'units' );
    end
end
%%

Contact us