%% Import data from text file.
% Script for importing data from the following text file:
%   C:\Users\gene\Documents\pH2020\Calcs\ph_profile_diagnostics.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 2020/07/17 09:49:35

%
%% Initialize variables.

filename = 'C:\Users\magene\Documents\pH2020\Calcs\ph_profile_diagnostics.txt';
delimiter = '\t';
startRow = 12;

%
%% Format for each line of text:
% column1: double (%f)

%	column2: double (%f)
%   column3: text (%s)
%	column4: double (%f)
%   column5: double (%f)
%	column6: double (%f)
%   column7: double (%f)
%	column8: double (%f)
%   column9: double (%f)
%	column10: double (%f)
%   column11: double (%f)
%	column12: double (%f)
%   column13: double (%f)
%	column14: double (%f)
%   column15: double (%f)
%	column16: double (%f)
%   column17: double (%f)
%	column18: double (%f)
%   column19: double (%f)
%	column20: double (%f)
%   column21: double (%f)
%	column22: double (%f)
%   column23: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%f%f%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%[^\n\r]';

%
%% Open the text file.

fileID = fopen(filename,'r');

%
%% Read columns of data according to the format.
% 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', delimiter, 'TextType', 'string', 'EmptyValue', NaN, 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');

%
%% Close the text file.

fclose(fileID);

%
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post processing code is included. To generate code which works for unimportable data, select unimportable cells in a file and regenerate the script.
%% Create output variable

JPData = table(dataArray{1:end-1}, 'VariableNames', {'Cruise','Station','JPDateTime','LonE','LatN','Pressuredbar','stSDN','pHSN','pHversion','AvgVrs','AvgVk','AvgstdVk','AvgIk','AvgIb','pHQC','pHoffset','Vrsoffset','Vkoffset','Ik_offset','Ib_offset','k0_Pon','k0_Poff','k0_HCl'});

%
%% Clear temporary variables

clearvars filename delimiter startRow formatSpec fileID dataArray ans;

%

DateTime = datetime(JPData{:,3}, 'InputFormat', 'yyyy-MM-dd HH:mm');
JPData = addvars(JPData, DateTime, 'After', 'JPDateTime');

avgStdVk = nonzeros(JPData.AvgstdVk);
medianAvgStdVk = median(avgStdVk, 'omitNaN')
meanAvgStdVk = mean(avgStdVk, 'omitNaN')

histogram(avgStdVk, 'BinLimits', [0 0.001], 'NumBins', 1000);