%Read each CSV file, export them to excel, change the text data in excel to numeric, import these excels and work with them
clear all
clc
%Where is the csv file
% cd 'C:\Documents\Excel'
% dataFolder = 'C:\Documents\Excel';
dirroot='C:\Documents\Excel';
for L=1:1:2
    dirname = [dirroot,'\L',num2str(L)];
    files = dir(fullfile(dirname,'*.csv'));              %Listing all files in the given directory with the extension of csv
    filename_list = {files.name};                        %We need only the filenames
    number_of_files = size(files,1);                     %Getting the number of csv files we have found
    for i=1:number_of_files                              %if there are csv files in the folder I run a for cycle on them
        filename = filename_list(i);                     %I pass the filename to a variable
        filedir = strcat(dirname,"\",filename);          %I extend the filename with the directory's path
        csv2table = readtable(filename(i));              %Give me the csv file in a table
        %fid = fopen(filename(i));
        csv2table2cell= table2cell(csv2table);           %Give me the table in cell
        xlswrite('filename.xls', csv2table2cell);        %Write me an excel from the cell
%Change the first two text data to 0 and 1 - because they aren't impotartant - to get a full numerical database
        varCell = {0,1};
        xlCell = {'A1','B1'};
        cellfun(@(v,c) xlswrite('*pmtf.xls',v,1,c),varCell,xlCell);
    end
end
%%
   for ii = 1: excel_files 
    data = cell2mat(num2cell(xlsread('csv2excel.xls')));  %read in the full-numeric excel file, convert it from double to cell, 
                                                          %from cell to matrix for the following calculations
    data_size = size(data);                               %The size of the matrix
    number_of_elements = numel(data(:,16));               %The 16th column elements
    rowsWith50 = find(any(data == 50, 2));                % Find out all the rows that have a 50 in them.
 % If any row(s) do, then find the column with the 50 in it, and get the max of that column.
    if ~isempty(rowsWith50)
        firstRowWith50 = data(rowsWith50(1), :);
        columnWith50 = find(firstRowWith50 == 50, 1, 'first');  % Get its column
        theMax = max(data(2:number_of_elements, columnWith50)); % Find the max of this column over all rows
        %Local_Max = findpeaks(data(2:number_of_elements, columnWith50));
    end



