clear all;
Open file
%filename = 'mpHOx_example_data.txt';
fid = fopen(filename, 'r');
if fid == -1
    error('Unable to open file: %s', filename);
end

% Initialize variables
data = {};
lineNumber = 0;

% Read lines from file
while true
    line = fgetl(fid);
    if line == -1
        break;
    end
    if contains(line,'//--')
        break
    end
    
    % Remove comments
    line = RemoveComments(line);
    
    % Split line into parameter, equipped, comments. This could be a
    % function
    parts = split(line, '= ', 2);
    if numel(parts) == 1
        continue; % Skip lines that don't have ' = ' delimiter
    elseif numel(parts) == 2 && strlength(parts(2)) > 1
        c = split(parts(2), ',', 2);
        parts = [parts(1) c];
    end
    
    % Trim whitespace
    parameter = strtrim(parts{1});
    equipped = strtrim(parts{2});
    if numel(parts) > 2
        comments = strtrim(parts{3});
    else
        comments = '';
    end
    % save data
    lineNumber = lineNumber + 1;
    data{lineNumber, 1} = parameter;
    data{lineNumber, 2} = equipped;
    data{lineNumber, 3} = comments;
end

% 
% for i = 1:height(data)
% 
% end
% 
% 

% Create table
dataTable = table(data(:,1),str2double(data(:,2)),data(:,3), 'VariableNames', {'parameter', 'equipped','comments'});
dataTable = dataTable(dataTable.equipped == 1, :);
while(1) % while statement and loop through a bunch of ifs???
    % Check if 'NanoFET pH' parameters are present
    if any(contains(dataTable.parameter, 'NanoFET pH'))
        % Select all equipped parameters
        equippedData = dataTable(dataTable.equipped == 1, :);
        
        % Remove parameters not relevant to NanoFET
        excludedParams = {'Battery Volt','Bias Battery Neg','Board Temperature','Board Humidity'};
        equippedData = equippedData(~contains(equippedData.parameter, excludedParams), :);
        
        % Append 'NanoFet' to parameter names
        equippedData.parameter = strcat(equippedData.parameter, ' NanoFet');
        
        % Concatenate modified data with original table
        dataTable = vertcat(dataTable, equippedData);
        
        % Remove parameters that are not equipped
        dataTable = dataTable(dataTable.equipped == 1, :);
    end
    if any(contains(dataTable.parameter,'Optode'))
        OpVars = {'Opt_PN','Opt_SN','Moxy','O2satper','TC_opt','Dphase','Bphase','Rphase','Bamp','Bpot','Ramp','Opt_rawtemp'}';
        dataTable = vertcat(dataTable, table(OpVars,ones(length(OpVars),1),repmat({''},length(OpVars),1),VariableNames={'parameter', 'equipped','comments'}));
        dataTable = dataTable(~contains(dataTable.parameter,'Optode'),:);
        dataTable = dataTable(dataTable.equipped==1,:); % remove parameters that are not equipped
    end
    if any(contains(dataTable.parameter,'Conductivity'))
        CTDVars = {'TC_MCat','Cond','Pres','pSal','Date_MC','Time_MC','Pump'}';
        dataTable = vertcat(dataTable, table(CTDVars,ones(length(CTDVars),1),repmat({''},length(CTDVars),1),VariableNames={'parameter', 'equipped','comments'}));
        dataTable = dataTable(~contains(dataTable.parameter,'Conductivity'),:);
        dataTable = dataTable(dataTable.equipped==1,:); % remove parameters that are not equipped
    end
    break
end

% if any(contains(dataTable.parameter,'NanoFET pH')) % execute if there is a nano attached to the mfet
%     dataTableNano = dataTable(dataTable.equipped==1,:); % select all equipped parameters
%     dataTableNano = dataTableNano(~contains(dataTableNano.parameter,...
%         {'Battery Volt','Bias Battery Neg','Board Temperature',...
%         'Board Humidity'}),:); % nano doesn't record these parameters
%     dataTableNano.parameter = strcat(dataTableNano.parameter,' NanoFet'); % name variables appropriatly
%     dataTable = vertcat(dataTable,dataTableNano); % create new table with mfet and nano parameters
%     dataTable = dataTable(dataTable.equipped==1,:); % remove parameters that are not equipped
% % add params for optode properties
% elseif any(contains(dataTable.parameter,'Optode'))
%     OpVars = {'Opt_PN','Opt_SN','Moxy','O2satper','TC_opt','Dphase','Bphase','Rphase','Bamp','Bpot','Ramp','Opt_rawtemp'}';
%     dataTable = vertcat(dataTable, table(OpVars,ones(length(OpVars),1),repmat({''},length(OpVars),1),VariableNames={'parameter', 'equipped','comments'}));
%     dataTable = dataTable(~contains(dataTable.parameter,'Optode'),:);
%     dataTable = dataTable(dataTable.equipped==1,:); % remove parameters that are not equipped
% % add params for ctd properties
% elseif any(contains(dataTable.parameter,'Conductivity'))
%     CTDVars = {'TC_MCat','Cond	Pres','pSal','Date_MC','Time_MC','Pump'};
%     dataTable = vertcat(dataTable, table(CTDVars,ones(length(CTDVars),1),repmat({''},length(CTDVars),1),VariableNames={'parameter', 'equipped','comments'}));
%     dataTable = dataTable(~contains(dataTable.parameter,'Conductivity'),:);
%     dataTable = dataTable(dataTable.equipped==1,:); % remove parameters that are not equipped
% else
%     dataTable = dataTable(dataTable.equipped==1,:);
% end


% Close file
fclose(fid);

% function line = RemoveComments(line)
%     % Remove comments indicated by '--' or '//'
%     index = strfind(line, '--');
%     if ~isempty(index)
%         line = line(index(1)+2:end);
%     else
%         index = strfind(line, '//');
%         if ~isempty(index)
%             line = line(index(1)+2:end);
%         end
%     end
% end

