%% Average and decimate regularly scattered data with fixed defined step % Prompt user for location of input file % Prompt user for step size for decimation of the averaged data % Group Y scattered data (only 1 Y column in a 2 column data set with X as first and Y as second column) in chunck and average it and decimate it according to the step size of decimation of X % Minimum and maximum are defined according to extremas in the X data and according to the decimation step size % Standard deviation is calculated for each step adn stored in additional column % A new result file is generated and saved in the same folder as the input file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Prompt user for input file [file,path,indx] = uigetfile('*.txt'); if isequal(file,0) disp('Error: the data treatment has been terminated.') return; end % get file name filename = [path file]; [fileID,errmsg] = fopen(filename); if isempty(errmsg) disp('Input file has been loaded correctly.') else disp('Error: the data treatment has been terminated.') return; end % Import data cdata = readtable(filename); fclose(fileID); % Check that there is at least 2 columns (because only the 2 first columns % are used for data treatment if length(cdata.Properties.VariableNames) < 2 disp('Error: input file data is wrong. The data treatment has been terminated.') return; end % Check that data is longer than 1 row if length(cdata{:,1}) > 1 && length(cdata{:,2}) > 1 disp('Input data is valid.') else disp('Error: wrong data. The data treatment has been terminated.') return; end % Prompt user for decimation step size prompt = {'Enter a value of decimation step size'}; dlgtitle = 'Delta'; definput = {'1'}; opts.Interpreter = 'tex'; answer = inputdlg(prompt,dlgtitle,[1 40],definput,opts); delta = str2double(answer); % convert answer into numerical % Get first point in X data and last point in X data first = min(cdata{:,1}); last = max(cdata{:,1}); distance = last - first; % Check that the step size is not zero and that it is not larger than the % distance between the first and last point if delta > 0 && delta < distance disp('Step size for decimation is valid.') else disp('Error: wrong step size for decimation. The data treatment has been terminated.') return end % round to the closest first data point or round lower according to % decimation step size starting_point = delta*floor(first/delta); % do the same for the last data point ending_point = delta*ceil(last/delta); % Because of numerical truncation, there might be one step too many at the % beginning and / or at the end. But this step will be empty or NaN X_output = (starting_point:delta:ending_point)'; Y_output = NaN(length(X_output),1); std_dev_output = NaN(length(X_output),1); for i = 1:length(X_output) % for each step in X_output, get the average of the Y data values % at distance delta/2 from the current X_output. selection_array = (cdata{:,1} >= X_output(i)-delta/2).*(cdata{:,1} <= X_output(i)+delta/2); % have to fulfill 2 conditions with ".*" sub_table=(cdata(logical(selection_array),2)); Y_output(i) = mean(sub_table{:,1}); std_dev_output(i) = std(sub_table{:,1}); end table_output = table(X_output,Y_output,std_dev_output); % Arrange all data in a table table_output.Properties.VariableNames([1 2]) = cdata.Properties.VariableNames([1 2]); % Rename table header with header of original input file % Write table data into text file in same folder as input file new_filename = [path 'avrg_decimated_std_dev_' file]; writetable(table_output,new_filename,'Delimiter','tab'); disp('Result file has been created in the same file as the input file.');