clear all; close all; clc

%% Import data from text file.
% Script for importing data from the following text file:
%
%    C:\Users\Jose Manuel\Documents\MEGAsync\Universidad\Cuarto\TFG\Matlab\horizons_results.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/03/14 16:16:17

%% Initialize variables.
filename = 'C:\Users\Jose Manuel\Documents\MEGAsync\Universidad\Cuarto\TFG\Matlab\horizons_results.txt';
startRow = 2;

%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%2s%2s%22s%4s%22s%4s%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.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);

%% Remove white space around all cell columns.
dataArray{1} = strtrim(dataArray{1});
dataArray{2} = strtrim(dataArray{2});
dataArray{4} = strtrim(dataArray{4});
dataArray{6} = strtrim(dataArray{6});

%% 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=[3,5,7]
    % 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 = textscan(strrep(numbers, ',', ''), '%f');
                numericData(row, col) = numbers{1};
                raw{row, col} = numbers{1};
            end
        catch me
        end
    end
end


%% Split data into numeric and cell columns.
rawNumericColumns = raw(:, [3,5,7]);
rawCellColumns = raw(:, [1,2,4,6]);


%% Exclude rows with non-numeric cells
I = ~all(cellfun(@(x) isnumeric(x) || islogical(x),rawNumericColumns),2); % Find rows with non-numeric cells
rawNumericColumns(I,:) = [];
rawCellColumns(I,:) = [];

%% Exclude rows with blank cells
I = any(cellfun(@(x) isempty(x) || (ischar(x) && all(x==' ')),rawNumericColumns),2); % Find row with blank cells
rawNumericColumns(I,:) = [];
rawCellColumns(I,:) = [];

%% Allocate imported array to column variable names
VarName1 = rawCellColumns(:, 1);
SO = rawCellColumns(:, 2);
E = cell2mat(rawNumericColumns(:, 1));
VarName4 = rawCellColumns(:, 3);
VarName5 = cell2mat(rawNumericColumns(:, 2));
VarName6 = rawCellColumns(:, 4);
VarName7 = cell2mat(rawNumericColumns(:, 3));


%% Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns I J K;


areal=[]
for i=4:4:length(E)
    areal=[areal ; E(i)];
end
areal;

ereal=[]
for i=1:4:length(E)
    ereal=[ereal ; E(i)];
end
ereal;

omreal=[]
for i=2:4:length(E)
    omreal=[omreal ; E(i)];
end
omreal;

wreal=[]
for i=2:4:length(VarName5)
    wreal=[wreal ; VarName5(i)];
end
wreal;

increal=[]
for i=1:4:length(VarName7)
    increal=[increal ; VarName7(i)];
end
increal;

tareal=[]
for i=3:4:length(VarName7)
    tareal=[tareal ; VarName7(i)];
end
tareal;

eor=[ereal areal increal omreal wreal tareal];

save elreal

