function varargout = Adsorption_Predict(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Adsorption_Predict_OpeningFcn, ...
                   'gui_OutputFcn',  @Adsorption_Predict_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before Adsorption_Predict is made visible.
function Adsorption_Predict_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Adsorption_Predict (see VARARGIN)

% Choose default command line output for Adsorption_Predict
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in Import.
function Import_Callback(hObject, eventdata, handles)

global Predict_Input savepath
[filename, pathname] = uigetfile({'*.xlsx','*.xls';});
savepath = pathname;
full_name = strcat(pathname,filename);
[Predict_Input] = xlsread(full_name);
set(handles.Path,'string',full_name);
set(handles.Rows,'string',num2str(size(Predict_Input,1)));
set(handles.Cols,'string',num2str(size(Predict_Input,2)));
TableData = get(handles.table,'data');
msgbox('Import is done');
set(handles.table,'data',Predict_Input)



% --- Executes on selection change in Selection.
function Selection_Callback(hObject, eventdata, handles)
% hObject    handle to Selection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global selection Selection materials
materials = get(handles.Selection,"Value");
switch materials
    case 1
        Selection = '0';
     case 2
        Selection = '1';
     case 3
        Selection = '2';
     case 4
        Selection = '3';
     case 5
        Selection = "4";
      end
msgbox('Selection is done');
selection =str2num(Selection);

% Hints: contents = cellstr(get(hObject,'String')) returns Selection contents as cell array
%        contents{get(hObject,'Value')} returns selected item from Selection

% --- Executes during object creation, after setting all properties.
function Selection_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Selection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in Mode.


% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
global nets inputcode outputcode Predict_Input selection Predicted_Results WorkNet;
load('TrainedANN');
WorkNet= TrainedANN;
%WorkNet = evalin('base','TrainedANN');
netbase = WorkNet{1,2};
incode = WorkNet{1,1};
outcode = WorkNet{1,3};

nets = netbase{1,selection};
inputcode   = incode{1,selection} ;
outputcode = outcode{1,selection} ;
TestInput        = mapminmax('apply',Predict_Input(:,1:8)',inputcode);
TestoutPred    = mapminmax('reverse',sim(nets,TestInput),outputcode);
Predicted_Results = TestoutPred';
msgbox('Calculation is done');
TableData      = get(handles.table,'data');
set(handles.table,'data',[Predict_Input,Predicted_Results]);

% --- Executes on button press in Export.
function Export_Callback(hObject, eventdata, handles)
global savepath
global Predicted_Results;
global Predict_Input;
Final_Result = [Predict_Input,Predicted_Results];
full_name = strcat(savepath,'PredictiedResults.csv');
csvwrite(full_name, Final_Result);
msgbox('File have been sent t current path: PredictedResults');

% --- Outputs from this function are returned to the command line.
function varargout = Adsorption_Predict_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

function Path_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function Path_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Rows_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function Rows_CreateFcn(hObject, ~, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function Cols_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function Cols_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Cols (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
