%% Import data from text file.
% Script for importing data from the following text file:
%
%    C:\Users\Gackolino\Desktop\16-00031-01_interpol.TXT
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.

% Auto-generated by MATLAB on 2016/05/11 21:20:31

clear  all

%% Initialize variables.
filename = 'C:\Users\Gackolino\Desktop\16-00031-01_interpol.TXT';
delimiter = ';';
startRow = 5;

%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%s%s%s%s%s%s%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
textscan(fileID, '%[^\n\r]', startRow-1, 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
    raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));

for col=[1,2,3,4,5,6,7,8,9,10]
    % Converts strings in the input cell array to numbers. Replaced non-numeric
    % strings with NaN.
    rawData = dataArray{col};
    for row=1:size(rawData, 1);
        % Create a regular expression to detect and remove non-numeric prefixes and
        % suffixes.
        regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\.]*)+[\,]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\.]*)*[\,]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
        try
            result = regexp(rawData{row}, regexstr, 'names');
            numbers = result.numbers;
            
            % Detected commas in non-thousand locations.
            invalidThousandsSeparator = false;
            if any(numbers=='.');
                thousandsRegExp = '^\d+?(\.\d{3})*\,{0,1}\d*$';
                if isempty(regexp(thousandsRegExp, '.', 'once'));
                    numbers = NaN;
                    invalidThousandsSeparator = true;
                end
            end
            % Convert numeric strings to numbers.
            if ~invalidThousandsSeparator;
                numbers = strrep(numbers, '.', '');
                numbers = strrep(numbers, ',', '.');
                numbers = textscan(numbers, '%f');
                numericData(row, col) = numbers{1};
                raw{row, col} = numbers{1};
            end
        catch me
        end
    end
end


%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells

%% Allocate imported array to column variable names
B1L = cell2mat(raw(1:40, 1));
B1T = cell2mat(raw(1:40, 2));
B2L = cell2mat(raw(1:40, 3));
B2T = cell2mat(raw(1:40, 4));
B3L = cell2mat(raw(1:40, 5));
B3T = cell2mat(raw(1:40, 6));
B4L = cell2mat(raw(1:40, 7));
B4T = cell2mat(raw(1:40, 8));
B5L = cell2mat(raw(1:40, 9));
B5T = cell2mat(raw(1:40, 10));
AL = cell2mat(raw(43:end, 1));
AT = cell2mat(raw(43:end, 2));

%% interpolation of each matrix
AT_int = min(AT):1:max(AT);
AL_int = interp1(AT,AL,AT_int);

B1T_int = min(B1T):1:max(B1T);
B1L_int = interp1(B1T,B1L,B1T_int);

B2T_int = min(B2T):1:max(B2T);
B2L_int = interp1(B2T,B2L,B2T_int);

B3T_int = min(B3T):1:max(B3T);
B3L_int = interp1(B3T,B3L,B3T_int);

B4T_int = min(B4T):1:max(B4T);
B4L_int = interp1(B4T,B4L,B4T_int);

B5T_int = min(B5T):1:max(B5T);
B5L_int = interp1(B5T,B5L,B5T_int);


%B1L_int2 = min(B1L_int):1:max(B1L_int);
%B1T_int2 = interp1(B1L_int,B1T_int2,B1L_int2);
%%Plot
%figure (1); plot(B1T_int,B1L_int);
%xlabel('Laufzeitänderung');ylabel('Kraft');grid on;

%% Turn colums 
% AT_int = rot90(AT_int);
% AL_int = rot90(AL_int);
% B1L_int = rot90(B1L_int);
% B1T_int = rot90(B1T_int);
% B2L_int = rot90(B2L_int);
% B2T_int = rot90(B2T_int);
% B3L_int = rot90(B3L_int);
% B3T_int = rot90(B3T_int);
% B4L_int = rot90(B4L_int);
% B4T_int = rot90(B4T_int);
% B5L_int = rot90(B5L_int);
% B5T_int = rot90(B5T_int);

%% Export to TXT
%dlmwrite(filename,M,delimiter,row,col)

%$gut dlmwrite ('D:\testdaten.txt',[AL_int; AT_int], 'delimiter', '\t');
%dlmwrite('dateiname.txt',[vec1 vec2 vec3],'delimiter','\t'

%   dlmwrite('D:\testdaten.txt',[AL_int; AT_int; B1L_int;%   B1T_int]','delimiter', '\t');


xlswrite(fid,AL_int);





% % Export reihenweise
% fid = fopen('D:\testdaten.txt', 'w');
% 
% for ii=1:2000;
%   fprintf( fid,'%f %f %f\n', AL_int(ii), AT_int{ii}, B2L_int(ii));
% end
% fclose( fid);
% 
% %% Ausgabe als TXT
% % Einpassen der Namen, Beschreibung und Einheiten
% 
% fid = fopen('D:\testdaten.txt', 'w');
% 
% % Schreiben von Text:
% % %s heisst String, \n erzeugt einen Zeilenumbruch
% fprintf(fid, '%s\n', 'Irgendein Text');
% 
% % Schreiben von numerischen Daten:
% % %g ist Fixkomma-Notation, mehrere Werte werden geschrieben
% % und mit Tabulator getrennt (\t).
% fprintf(fid, '%g\t', pi, [7.5:3:13.5]);
% fprintf(fid, '\n'); % nun fehlt noch der Zeilenumbruch
% 
% % Ausgabe mit max. 6 Vorkomma- und 2 Nachkommastellen
% fprintf(fid, '%6.2f\t', [737.5:3234.273:20000]);
% 
% % Datei schliessen
% fclose(fid);





%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me R;