function varargout = nbcs(varargin)
% NBCS MATLAB code for nbcs.fig
%      NBCS, by itself, creates a new NBCS or raises the existing
%      singleton*.
%
%      H = NBCS returns the handle to a new NBCS or the handle to
%      the existing singleton*.
%
%      NBCS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NBCS.M with the given input arguments.
%
%      NBCS('Property','Value',...) creates a new NBCS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before nbcs_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to nbcs_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help nbcs

% Last Modified by GUIDE v2.5 11-May-2017 18:59:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @nbcs_OpeningFcn, ...
                   'gui_OutputFcn',  @nbcs_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 nbcs is made visible.
function nbcs_OpeningFcn(hObject, eventdata, 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 nbcs (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

warning('off');

%%% add path of freesurfer matlab codes
currp = which('nbcs.m');
currp = fileparts(currp);
addpath([currp,filesep,'Codes_FS']);

%%% load nbcs figure
Imgdata  =  imread([currp,filesep,'nbcs.jpg']);
axes(handles.axes1);
imshow(Imgdata);
% pos     = get(handles.axes1,'pos');
% text(pos(3)/5,pos(4),'NBCS','FontSize',20,'FontName','Wide Latin','Color',[0 0 0]);

% set data reading off
set(handles.checkbox2,   'Enable','off');
set(handles.checkbox3,   'Enable','off');
set(handles.checkbox15,  'Enable','off');
set(handles.pushbutton1, 'Enable','off');
set(handles.edit1,       'Enable','off');
set(handles.popupmenu1,  'Enable','off');
set(handles.popupmenu2,  'Enable','off');
set(handles.popupmenu3,  'Enable','off');
set(handles.checkbox4,   'Enable','off');
set(handles.listbox1,    'Enable','off');
set(handles.pushbutton7, 'Enable','off');
set(handles.edit8,       'Enable','off');
set(handles.text14,      'Enable','off');
set(handles.edit7,       'Enable','off');
set(handles.pushbutton5, 'Enable','off');

% set matrix building off
set(handles.checkbox5,    'Enable','off');
set(handles.checkbox6,    'Enable','off');
set(handles.text15,       'Enable','off');
set(handles.edit2,        'Enable','off');
set(handles.checkbox16,   'Enable','off');
set(handles.radiobutton1, 'Enable','off');
set(handles.radiobutton3, 'Enable','off');
set(handles.radiobutton4, 'Enable','off');
set(handles.radiobutton5, 'Enable','off');
set(handles.radiobutton6, 'Enable','off');
set(handles.radiobutton7, 'Enable','off');
set(handles.radiobutton8, 'Enable','off');
set(handles.radiobutton9, 'Enable','off');
set(handles.radiobutton10,'Enable','off');
set(handles.radiobutton11,'Enable','off');
set(handles.radiobutton12,'Enable','off');
set(handles.checkbox17,   'Enable','off');
set(handles.edit9,        'Enable','off');
set(handles.pushbutton4,  'Enable','off');

% set clustering off
set(handles.checkbox10,   'Enable','off');
set(handles.checkbox13,   'Enable','off');
set(handles.checkbox14,   'Enable','off');
set(handles.text8,        'Enable','off');
set(handles.text7,        'Enable','off');
set(handles.text6,        'Enable','off');
set(handles.edit3,        'Enable','off');
set(handles.edit5,        'Enable','off');
set(handles.edit6,        'Enable','off');
set(handles.checkbox18,   'Enable','off');
set(handles.checkbox19,   'Enable','off');
set(handles.edit10,       'Enable','off');
set(handles.pushbutton6,  'Enable','off');

% UIWAIT makes nbcs wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = nbcs_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;


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox1,'value')
    set(handles.checkbox2,   'Enable','on');
    set(handles.checkbox3,   'Enable','on');
    set(handles.checkbox15,  'Enable','on');
    set(handles.pushbutton1, 'Enable','on');
    set(handles.edit1,       'Enable','on');
    set(handles.popupmenu1,  'Enable','on');
    set(handles.popupmenu2,  'Enable','on');
    set(handles.popupmenu3,  'Enable','on');
    set(handles.checkbox4,   'Enable','on');
    set(handles.listbox1,    'Enable','on');
    set(handles.pushbutton7, 'Enable','on');
    set(handles.edit8,       'Enable','on');
    set(handles.text14,      'Enable','on');
    set(handles.edit7,       'Enable','on');
    set(handles.pushbutton5, 'Enable','on');
else
    set(handles.checkbox2,   'Enable','off'); set(handles.checkbox2,  'value',0); 
    set(handles.checkbox3,   'Enable','off'); set(handles.checkbox3,  'value',0); 
    set(handles.checkbox15,  'Enable','off'); set(handles.checkbox15, 'value',0);
    set(handles.pushbutton1, 'Enable','off');
    set(handles.edit1,       'Enable','off');
    set(handles.popupmenu1,  'value',     1); set(handles.popupmenu1, 'Enable','off'); set(handles.popupmenu1,'string','Atlas'); 
    set(handles.popupmenu2,  'value',     1); set(handles.popupmenu2, 'Enable','off'); set(handles.popupmenu2,'string','Measure');
    set(handles.popupmenu3,  'value',     1); set(handles.popupmenu3, 'Enable','off'); set(handles.popupmenu3,'string','Smooth');
    set(handles.checkbox4,   'Enable','off'); set(handles.checkbox4,  'value',0);
    set(handles.listbox1,    'Enable','off'); set(handles.listbox1,   'string',[]);
    set(handles.pushbutton7, 'Enable','off');
    set(handles.edit8,       'Enable','off');
    set(handles.text14,      'Enable','off');
    set(handles.edit7,       'Enable','off');
    set(handles.pushbutton5, 'Enable','off');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox1


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

currp = which('nbcs.m');
currp = fileparts(currp);

if get(handles.checkbox2,'value')
    [Imgs_nii,ext_nii]  = getdirfile_long([currp, filesep,'Atlas',filesep,'NIFTI'],'nii','Onlyfile');
    [Imgs_img,ext_img]  = getdirfile_long([currp, filesep,'Atlas',filesep,'NIFTI'],'img','Onlyfile');
    Imgs                = cat(1,Imgs_nii,Imgs_img);
    ext                 = cat(1,ext_nii,ext_img);
    clear Imgs_nii ext_nii Imgs_img ext_img
    for i=1:length(ext)
        ext{i} = ext{i}(1:end-4);
    end
    clear i
%     setappdata(0,'dr_atlas_img',Imgs); clear Imgs
%     setappdata(0,'dr_nifti',1); % 1- read nifti data
    set(handles.checkbox3, 'Enable','off');
    set(handles.checkbox15,'Enable','off');
    set(handles.popupmenu1,'string',  ext);
    set(handles.popupmenu1,'value',     1);
    set(handles.popupmenu2,'Enable','off');
    set(handles.popupmenu3,'Enable','off');
    
%     setappdata(0,'dr_atlas',get(handles.popupmenu1,'value'));
else
%     setappdata(0,'dr_nifti',0); % 0- do not read nifti data
    set(handles.checkbox3, 'Enable','on');
    set(handles.checkbox15,'Enable','on');
    set(handles.popupmenu1,'value',    1); set(handles.popupmenu1,'string',{'Atlas'});
    set(handles.popupmenu2,'Enable','on');
    set(handles.popupmenu3,'Enable','on');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox2


% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
currp = which('nbcs.m');
currp = fileparts(currp);

if get(handles.checkbox3,'value')
    subs        = dir([currp,filesep,'Atlas',filesep,'FREESURFER']);
    subs(1:2)   = [];
    Imgs        = cell(0);
    ext         = cell(0);
    for i=1:length(subs)
        Imgs{i} = [currp,filesep,'Atlas',filesep,'FREESURFER',filesep,subs(i).name];
        ext{i}  = subs(i).name;
    end
    ext{end+1}  = 'vertexwise';
    clear i subs 
    
%     setappdata(0,'dr_atlas_img',Imgs);
%     setappdata(0,'dr_freesurfer',1);
    set(handles.checkbox2, 'Enable','off');
    set(handles.checkbox15,'Enable','off');
    set(handles.popupmenu1,'value',     1); set(handles.popupmenu1,'string',ext);
    set(handles.popupmenu2,'value',     1); set(handles.popupmenu2,'string',{'thickness','area','sulc','curv','pial_lgi'});
    set(handles.popupmenu3,'value',     1); set(handles.popupmenu3,'string',{'0','5','10','15','20','25'});
    
%     setappdata(0,'dr_atlas',get(handles.popupmenu1,'value'));
%     string_ms = get(handles.popupmenu2,'string');setappdata(0,'dr_measure',string_ms{get(handles.popupmenu2,'value')}); clear string_ms
%     string_ms = get(handles.popupmenu3,'string');setappdata(0,'dr_smooth',string_ms{get(handles.popupmenu3,'value')}); clear string_ms
else
%     setappdata(0,'dr_freesurfer',0);
    set(handles.checkbox2, 'Enable','on');
    set(handles.checkbox15,'Enable','on');
    set(handles.popupmenu1,'value',    1); set(handles.popupmenu1,'string',{'Atlas'});
    set(handles.popupmenu2,'value',    1); set(handles.popupmenu2,'string',{'Measure'});
    set(handles.popupmenu3,'value',    1); set(handles.popupmenu3,'string',{'Smooth'});
end
% Hint: get(hObject,'Value') returns toggle state of checkbox3


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
folder_path = uigetdir(pwd);
set(handles.edit1,'string',folder_path);
clear folder_name


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% setappdata(0,'dr_atlas',get(handles.popupmenu1,'value'));
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% string_ms = get(handles.popupmenu2,'string');
% setappdata(0,'dr_measure',string_ms{get(handles.popupmenu2,'value')});
% % fprintf('measurement : %s \n',string_ms{get(handles.popupmenu2,'value')});
% clear string_ms
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% string_ms = get(handles.popupmenu3,'string');
% setappdata(0,'dr_smooth',string_ms{get(handles.popupmenu3,'value')});
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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


% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox4,'value')
    [cov_file,cov_path] = uigetfile({'*.mat';'*.txt';'*.xls';'*.xlsx'},'Select covariate file');
    [~,~,ext]           = fileparts([cov_path,filesep,cov_file]);
    if  isequal(ext,'.txt')
        covs            = load([cov_path,filesep,cov_file]);
    elseif isequal(ext,'.mat')
        load([cov_path,filesep,cov_file]);
        covs            = whos('-file',[cov_path,filesep,cov_file]);
        covs            = eval(covs.name);
    elseif isequal(ext,'.xls') || isequal(ext,'.xlsx')
        covs            = xlsread([cov_path,filesep,cov_file]);
    end
    set(handles.listbox1,'string',num2str(covs));
    clear cov_file cov_path
else
    set(handles.listbox1,'string',[]); 
end
% Hint: get(hObject,'Value') returns toggle state of checkbox4


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in checkbox8.
function checkbox8_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if get(handles.checkbox8,'value')
    set(handles.checkbox5,    'Enable','on');
    set(handles.checkbox6,    'Enable','on');
    set(handles.text15,       'Enable','on');
    set(handles.edit2,        'Enable','on');
    set(handles.checkbox16,   'Enable','on');
    set(handles.radiobutton1, 'Enable','on');
    set(handles.radiobutton3, 'Enable','on');
    set(handles.radiobutton4, 'Enable','on');
    set(handles.radiobutton5, 'Enable','on');
    set(handles.radiobutton6, 'Enable','on');
    set(handles.radiobutton7, 'Enable','on');
    set(handles.radiobutton8, 'Enable','on');
    set(handles.radiobutton9, 'Enable','on');
    set(handles.radiobutton10,'Enable','on');
    set(handles.radiobutton11,'Enable','on');
    set(handles.radiobutton12,'Enable','on');
    set(handles.checkbox17,   'Enable','on');
    set(handles.edit9,        'Enable','on');
    set(handles.pushbutton4,  'Enable','on');
    
    matpath      = uiget_multi(pwd, 'Please select data matrix(s)');
    matmatrix    = [];
    disp('Reading raw data matrix');
    for i=1:length(matpath)
        load(matpath{i});
        A                 = whos('-file',matpath{i});
        for j = 1:length(A)
            if isequal(A(j).class,'double')
                A         = eval(A(j).name);
                matmatrix = [matmatrix,A];
            end
        end
        clear A
    end
    setappdata(0,'matmatrix',matmatrix);
    figure;
    imagesc(matmatrix); colorbar;
    title('Raw data matrix');
    clear matmatrix i j
else
    set(handles.checkbox5,    'Enable','off'); set(handles.checkbox5,'value',0); 
    set(handles.checkbox6,    'Enable','off'); set(handles.checkbox6,'value',0); 
    set(handles.text15,       'Enable','off');
    set(handles.edit2,        'Enable','off');
    set(handles.checkbox16,   'Enable','off');
    set(handles.radiobutton1, 'Enable','off');
    set(handles.radiobutton3, 'Enable','off');
    set(handles.radiobutton4, 'Enable','off');
    set(handles.radiobutton5, 'Enable','off');
    set(handles.radiobutton6, 'Enable','off');
    set(handles.radiobutton7, 'Enable','off');
    set(handles.radiobutton8, 'Enable','off');
    set(handles.radiobutton9, 'Enable','off');
    set(handles.radiobutton10,'Enable','off');
    set(handles.radiobutton11,'Enable','off');
    set(handles.radiobutton12,'Enable','off');
    set(handles.checkbox17,   'Enable','off');
    set(handles.edit9,        'Enable','off');
    set(handles.pushbutton4,  'Enable','off');
    setappdata(0,'matmatrix',[]);
end
% Hint: get(hObject,'Value') returns toggle state of checkbox8


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

matmatrix    = getappdata(0,'matmatrix'); % get raw data matrix
outdir       = get(handles.edit9,'string');
mb_ssim      = get(handles.checkbox5,'value'); % SSIM
mb_pca       = get(handles.checkbox6,'value'); % PCA

if mb_ssim % SSIM
    dismatrix_data = dismatrix_ssim(matmatrix');
    figure;
    imagesc(dismatrix_data); colorbar;
    title('SSIM-based distance matrix');
    if ~isempty(outdir)
        save([outdir,filesep,'dismatrix_data.mat'],'dismatrix_data');
    end
else
    mb_algorithm         = getappdata(0,'mb_algorithm');
    if ~mb_pca % do not perform pca
        dismatrix_data   = pdist(matmatrix,mb_algorithm);
        dismatrix_data   = squareform(dismatrix_data);
        figure;
        imagesc(dismatrix_data); colorbar;
        title(['distance matrix - ',mb_algorithm]);
        if ~isempty(outdir)
            save([outdir,filesep,'dismatrix_data.mat'],'dismatrix_data');
        end
    else % do pca
        mb_comp      = get(handles.edit2,'string');
        if isequal(mb_comp,'1.0')
            mb_comp   = str2num(mb_comp);
            comptype  = 'percent'; 
        else
            mb_comp   = str2num(mb_comp);
            if mb_comp > 0 && mb_comp < 1
                comptype  = 'percent'; % accumulative contribution rate
            elseif mb_comp == fix(mb_comp)
                comptype  = 'number'; % number of components
            else
                error('The Components arg must be 0-1 or positive integer');
            end
        end
        
        if ~get(handles.checkbox16,'value') % not back to raw data space
            pcacomp        = dismatrix_pca(matmatrix,mb_comp,comptype);
            dismatrix_data = pdist(pcacomp,mb_algorithm);
            dismatrix_data = squareform(dismatrix_data);
            fprintf('Using the components (in principle component space) for matrix building \n');
            if isequal(comptype,'percent')
                fprintf('The accumulative contribution rate is: %d/100 \n',mb_comp*100);
            elseif isequal(comptype,'number')
                fprintf('The number of components is: %d \n',mb_comp);
            end
            fprintf('The algorithm: %s \n',mb_algorithm);
            figure;
            imagesc(dismatrix_data); colorbar;
            title(['PCA-based distance matrix - ',mb_algorithm]);
        else
            [~,pcaraw]     = dismatrix_pca(matmatrix,mb_comp,comptype); % back to raw data space
            dismatrix_data = pdist(pcaraw,mb_algorithm);
            dismatrix_data = squareform(dismatrix_data);
            fprintf('Using the components with back to raw data space for matrix building \n');
            if isequal(comptype,'percent')
                fprintf('The accumulative contribution rate is: %d/100 \n',mb_comp*100);
            elseif isequal(comptype,'number')
                fprintf('The number of components is: %d \n',mb_comp);
            end
            fprintf('The algorithm: %s \n',mb_algorithm);
            figure;
            imagesc(dismatrix_data); colorbar;
            title(['PCA(raw data space)-based distance matrix - ',mb_algorithm]);
        end
       if ~isempty(outdir)
            save([outdir,filesep,'dismatrix_data.mat'],'dismatrix_data');
        end
    end
end




function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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
set(hObject,'string',0.9);
setappdata(0,'mb_comp',0.9);


% --- Executes on button press in checkbox6.
function checkbox6_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox6,  'value')
    set(handles.checkbox5, 'Enable','off');
else
    set(handles.checkbox5, 'Enable','on');
    set(handles.checkbox16,'value',    0);
end
% Hint: get(hObject,'Value') returns toggle state of checkbox6


% --- Executes on button press in checkbox5.
function checkbox5_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox5,'value')
    set(handles.checkbox6,    'Enable','off');
    set(handles.text15,       'Enable','off');
    set(handles.edit2,        'Enable','off');
    set(handles.checkbox16,   'Enable','off');
    set(handles.radiobutton1, 'Enable','off');
    set(handles.radiobutton3, 'Enable','off');
    set(handles.radiobutton4, 'Enable','off');
    set(handles.radiobutton5, 'Enable','off');
    set(handles.radiobutton6, 'Enable','off');
    set(handles.radiobutton7, 'Enable','off');
    set(handles.radiobutton8, 'Enable','off');
    set(handles.radiobutton9, 'Enable','off');
    set(handles.radiobutton10,'Enable','off');
    set(handles.radiobutton11,'Enable','off');
    set(handles.radiobutton12,'Enable','off');
else
    set(handles.checkbox6,    'Enable','on');
    set(handles.text15,       'Enable','on');
    set(handles.edit2,        'Enable','on');
    set(handles.checkbox16,   'Enable','on');
    set(handles.radiobutton1, 'Enable','on');
    set(handles.radiobutton3, 'Enable','on');
    set(handles.radiobutton4, 'Enable','on');
    set(handles.radiobutton5, 'Enable','on');
    set(handles.radiobutton6, 'Enable','on');
    set(handles.radiobutton7, 'Enable','on');
    set(handles.radiobutton8, 'Enable','on');
    set(handles.radiobutton9, 'Enable','on');
    set(handles.radiobutton10,'Enable','on');
    set(handles.radiobutton11,'Enable','on');
    set(handles.radiobutton12,'Enable','on');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox5


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
currpath     = which('nbcs.m');
currpath     = fileparts(currpath);

dr_nifti     = get(handles.checkbox2,            'value'); % 1 - yes, reading nifti
dr_fs        = get(handles.checkbox3,            'value'); % 1 - yes, reading freesurfer
dr_mat       = get(handles.checkbox15,           'value'); % 1-yes, reading mat
dr_fold      = get(handles.edit1,                'string'); % data folder
dr_out       = get(handles.edit8,                'string'); % output folder
dr_prefix    = get(handles.edit7,                'string'); % prefix

% fprintf('nifti data : %d \n',dr_nifti);
% fprintf('nifti data : %d \n',dr_fs);

if dr_nifti
    dr_atlas     = get(handles.popupmenu1,           'string');
    dr_atlas     = dr_atlas{get(handles.popupmenu1,  'value')}; % atlas
    
    atlas   = dir([currpath,filesep,'Atlas',filesep,'NIFTI',filesep,dr_atlas,'*']);
    atlas   = [currpath,filesep,'Atlas',filesep,'NIFTI',filesep,atlas(1).name];
    fprintf('The atlas is : %s \n',atlas);
    
    [~,voxelcheck] = fileparts(atlas);
    if ~isempty(strfind(voxelcheck,'voxelwise')) || ~isempty(strfind(voxelcheck,'Voxelwise'))
        voxelwise = 1;
    else
        voxelwise = 0;
    end
    clear voxelcheck
    
    V1       = spm_vol(atlas);
    Imgs     = dir([dr_fold,filesep,'*.nii']);
    if isempty(Imgs)
        Imgs = dir([dr_fold,filesep,'*.img']);
    end
    V2 = spm_vol([dr_fold,filesep,Imgs(1).name]);
    
    if ~all(V1.dim == V2.dim) || ~all(abs(diag(V1.mat(1:3,1:3))) == abs(diag(V2.mat(1:3,1:3))))
        fprintf('Resample the atlas into %d x %d x %d. Waiting ... \n',abs(V2.mat(1,1)),abs(V2.mat(2,2)),abs(V2.mat(3,3)));
        InputFile            = atlas;
        [~,atl_name,atl_ext] = fileparts(atlas);
        OutputFile           = [dr_out,filesep,['Resample_',atl_name,atl_ext]];
        Image_reslice(InputFile,OutputFile,abs(diag(V2.mat(1:3,1:3)))',0, [dr_fold,filesep,Imgs(1).name]);
        atlas                = OutputFile;
        clear InputFile atl_name atl_ext OutputFile
    end
    
    [dataread_data, yes, data_name] = dataread_nifti(dr_fold,atlas,voxelwise); 
    clear atlas V1 V2
end
% 
if dr_fs
    dr_atlas     = get(handles.popupmenu1,           'string');
    dr_atlas     = dr_atlas{get(handles.popupmenu1,  'value')}; % atlas
    dr_measure   = get(handles.popupmenu2,           'string');
    dr_measure   = dr_measure{get(handles.popupmenu2,'value')}; % measurement
    dr_smooth    = get(handles.popupmenu3,           'string');
    dr_smooth    = dr_smooth{get(handles.popupmenu3, 'value')}; % smooth kernal
    
    if ~isequal(dr_atlas,'vertexwise')
        atlas = [currpath,filesep,'Atlas',filesep,'FREESURFER',filesep,dr_atlas];
        vertexwise = 0;
    else
        atlas = 'vertexwise (whole brain)';
        vertexwise = 1;
    end
    
    fprintf('The atlas : %s \n',atlas);
    fprintf('The measurement : %s \n',dr_measure);
    fprintf('The smooth kernel : %s \n',dr_smooth);
    [dataread_data, yes, data_name] = dataread_freesurfer(dr_fold,dr_measure,dr_smooth,atlas,vertexwise);
end
% 
if dr_mat
    [mats,data_name] = getdirfile_long(dr_fold,'mat','Onlyfile');
    load(mats{1}); 
    A                = whos('-file',mats{1}); 
    A                = eval(A.name);
    if size(A,1) ~= size(A,2)
        error('This toolbox only read MAT data with square matrix');
    end
    A(isnan(A))         = 0;
    if sum(sum(A-A')) < 10^-6
        if ~any(diag(A))
            Aind = find(triu(ones(size(A)),1)); % symmetric and zero-diagonal
            mattype = 'symmetric and zero-diagonal';
        else
            Aind = find(triu(ones(size(A)))); % symmetric and nonzero-diagonal
            mattype = 'symmetric and nonzero-diagonal';
        end
    else
        if ~any(diag(A))
            Aind = ones(size(A)); Aind(1:size(Aind,1)+1:end) = 0;
            Aind = find(Aind); % none symmetric and zero-diagonal
            mattype = 'nonsymmetric and zero-diagonal';
        else
            Aind = find(ones(size(A))); % none symmetric and nonzero-diagonal
            mattype = 'nonsymmetric and nonzero-diagonal';
        end
    end
    dataread_data = zeros([length(length(mats)),size(A)]);
    clear A
    
    for i=1:length(mats)
        fprintf('Reading %s MAT file : %s \n',mattype,mats{i});
        load(mats{i});
        A = whos('-file',mats{i});
        dataread_data(i,:,:) = eval(A.name);
        clear A
    end
    clear i
    dataread_data = reshape(dataread_data,size(dataread_data,1),size(dataread_data,2)*size(dataread_data,3));
    dataread_data = dataread_data(:,Aind);
    yes = 1; 
    clear mats Aind 
end
% 
if get(handles.checkbox4,'value')
    covs  = str2num(get(handles.checkbox4,'string'));
    fprintf('Regressing out covariates. Pls wait...... \n');
    if ~isempty(covs)
        for icov = 1:size(dataread_data,2)
            sig = dataread_data(:,icov);
            B   = glmfit(covs,sig,'normal');
            dataread_data(:,icov) = sig - covs*B(2:end);
        end
        clear sig B icov
    end
end

if yes
    if exist('data_name','var')
        save([dr_out,filesep,dr_prefix,'_dataread.mat'],'dataread_data','data_name');
    else
        save([dr_out,filesep,dr_prefix,'_dataread.mat'],'dataread_data');
    end
    disp('Reading data is finished \n');
else
    error('Data-Reading got wrong. Pls Check');
end

% --- Executes on button press in checkbox11.
function checkbox11_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if get(handles.checkbox11,'value')
    set(handles.checkbox10, 'Enable','on');
    set(handles.checkbox13, 'Enable','on');
    set(handles.checkbox14, 'Enable','on');
    set(handles.text8,      'Enable','on');
    set(handles.text7,      'Enable','on');
    set(handles.text6,      'Enable','on');
    set(handles.edit3,      'Enable','on');
    set(handles.edit5,      'Enable','on');
    set(handles.edit6,      'Enable','on');
    set(handles.checkbox19, 'Enable','on');
    set(handles.checkbox18, 'Enable','on');
    set(handles.edit10,     'Enable','on');
    set(handles.pushbutton6,'Enable','on');
    
    matpath      = uiget_multi(pwd, 'Please select distance matrix(s)');
    disp('Reading distance matrix');
    load(matpath{1});
    A            = whos('-file',matpath{1});
    dismatrix    = eval(A.name);
    clear A
    setappdata(0,'dismatrix',dismatrix); % save dismatrix
    figure;
    imagesc(dismatrix); colorbar;
    title('Distance matrix');
    clear dismatrix i j
else
    set(handles.checkbox10, 'Enable','off'); set(handles.checkbox10,'value',0); setappdata(0,'cl_kmeans',0);
    set(handles.checkbox13, 'Enable','off'); set(handles.checkbox13,'value',0); setappdata(0,'cl_kmedoids',0);
    set(handles.checkbox14, 'Enable','off'); set(handles.checkbox14,'value',0); setappdata(0,'cl_density',0);
    set(handles.text8,      'Enable','off');
    set(handles.text7,      'Enable','off');
    set(handles.text6,      'Enable','off');
    set(handles.edit3,      'Enable','off');
    set(handles.edit5,      'Enable','off');
    set(handles.edit6,      'Enable','off');
    set(handles.checkbox19, 'Enable','off'); set(handles.checkbox19,'value',0);
    set(handles.checkbox18, 'Enable','off');
    set(handles.edit10,     'Enable','off');
    set(handles.pushbutton6,'Enable','off');
    setappdata(0,'dismatrix',[]);
end
% Hint: get(hObject,'Value') returns toggle state of checkbox11


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
currpath = which('nbcs.m');
currpath = fileparts(currpath);

dismatrix     = getappdata(0,      'dismatrix');
outdir        = get(handles.edit10,'string');
cl_kmeans     = get(handles.checkbox10,'value');
cl_kmedoids   = get(handles.checkbox13,'value');
cl_density    = get(handles.checkbox14,'value');

cl_para_kmean = str2num(get(handles.edit3,'string'));
cl_para_kmedo = str2num(get(handles.edit5,'string'));
cl_para_densi = str2num(get(handles.edit3,'string'));

if cl_kmeans
    fprintf('Clustering using Kmeans. Number of clusters - %d \n',cl_para_kmean);
    assig = kmeans(dismatrix,cl_para_kmean)';
    if ~isempty(outdir)
        save([outdir,filesep,'clusterassign_data.mat'],'assig');
    end
end

if cl_kmedoids
    fprintf('Clustering using Kmedoids. Number of clusters - %d \n',cl_para_kmedo);
    assig = kmedoids(dismatrix,cl_para_kmedo)';
    if ~isempty(outdir)
        save([outdir,filesep,'clusterassign_data.mat'],'assig');
    end
end

if cl_density
    fprintf('Density-based clustering. The parameter is : %f \n',cl_para_densi);
    [~, ~, assig] = clustering_lzl(dismatrix,cl_para_densi);
    if ~isempty(outdir)
        save([outdir,filesep,'clusterassign_data.mat'],'assig');
    end
end

Ind          = []; % labels after sorting
IndNum       = []; % Number of elements per subtype
LabelClust   = unique(assig);
for i=1:length(LabelClust)
    IndNum   = [IndNum;length(find(assig == LabelClust(i)))];
    Ind      = [Ind,find(assig == LabelClust(i))];
end

B            = dismatrix(Ind,Ind);

figure; 
imagesc(B); colorbar; 
axis([1 size(B,1) 1 size(B,1)]);hold on; 
title('Distance matrix after sorting');%
if get(handles.checkbox19,'value')
    IndNum       = cumsum(IndNum);
    IndNum       = [1;IndNum];
    if length(IndNum) > 2
        for i=1:length(IndNum)
            if i ==1
                plot([IndNum(i):IndNum(i+1)],repmat(IndNum(i),length(IndNum(i):IndNum(i+1)),1),'k--','LineWidth',1.5); hold on;
                plot(repmat(IndNum(i),length(IndNum(i):IndNum(i+1)),1),[IndNum(i):IndNum(i+1)],'k--','LineWidth',1.5); hold on;
            elseif i==length(IndNum)
                plot(IndNum(i-1):IndNum(i),repmat(IndNum(i),length(IndNum(i-1):IndNum(i)),1),'k--','LineWidth',1.5); hold on;
                plot(repmat(IndNum(i),length(IndNum(i-1):IndNum(i)),1),IndNum(i-1):IndNum(i),'k--','LineWidth',1.5); hold on;
            else
                plot([IndNum(i-1):IndNum(i+1)],repmat(IndNum(i),length(IndNum(i-1):IndNum(i+1)),1)+0.5,'k--','LineWidth',1.5); hold on;
                plot(repmat(IndNum(i),length(IndNum(i-1):IndNum(i+1)),1)+0.5,[IndNum(i-1):IndNum(i+1)],'k--','LineWidth',1.5); hold on;
            end
        end
    end
end
clear  LabelClust assig Ind IndNum B i dis_matrix  IndNum
    

% --- Executes on button press in checkbox10.
function checkbox10_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox10,'value')
    set(handles.checkbox13,'Enable','off');
    set(handles.checkbox14,'Enable','off');
    set(handles.edit5,     'Enable','off');
    set(handles.edit6,     'Enable','off');
else
    set(handles.checkbox13,'Enable','on');
    set(handles.checkbox14,'Enable','on');
    set(handles.edit5,     'Enable','on');
    set(handles.edit6,     'Enable','on');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox10



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cl_para_kmean = str2double(get(handles.edit3,'String'));
% fprintf('number of components : %f \n',n_comp);
if cl_para_kmean <= 0 || fix(cl_para_kmean) ~= cl_para_kmean
    error('The parameter must be positive integer \n');
end
setappdata(0,'cl_para_kmean',cl_para_kmean);
% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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
set(hObject,'string',3);
setappdata(0,'cl_para_kmean',3);


function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cl_para_kmedo = str2double(get(handles.edit5,'String'));
% fprintf('number of components : %f \n',n_comp);
if cl_para_kmedo <= 0 || fix(cl_para_kmedo) ~= cl_para_kmedo
    error('The parameter must be positive integer \n');
end
setappdata(0,'cl_para_kmedo',cl_para_kmedo);
% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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
set(hObject,'string',3);
setappdata(0,'cl_para_kmedo',3);

% --- Executes on button press in checkbox13.
function checkbox13_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox13,'value')
    set(handles.checkbox10,'Enable','off');
    set(handles.checkbox14,'Enable','off');
    set(handles.edit3,     'Enable','off');
    set(handles.edit6,     'Enable','off');
else
    set(handles.checkbox10,'Enable','on');
    set(handles.checkbox14,'Enable','on');
    set(handles.edit3,     'Enable','on');
    set(handles.edit6,     'Enable','on');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox13



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cl_para_densi = str2double(get(handles.edit6,'String'));
% fprintf('number of components : %f \n',n_comp);
if cl_para_densi <= 0 || cl_para_densi > 1
    error('The parameter must be 0 - 1 \n');
end
setappdata(0,'cl_para_densi',cl_para_densi);
% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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
set(hObject,'string',0.1);
setappdata(0,'cl_para_densi',0.1);

% --- Executes on button press in checkbox14.
function checkbox14_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox14,'value')
    set(handles.checkbox10,'Enable','off');
    set(handles.checkbox13,'Enable','off');
    set(handles.edit3,     'Enable','off');
    set(handles.edit5,     'Enable','off');
else
    set(handles.checkbox10,'Enable','on');
    set(handles.checkbox13,'Enable','on');
    set(handles.edit3,     'Enable','on');
    set(handles.edit5,     'Enable','on');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox14


% --- Executes during object creation, after setting all properties.
function checkbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% setappdata(0,'dr_nifti',0); % 1- read nifti data


% --- Executes during object creation, after setting all properties.
function checkbox3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% setappdata(0,'dr_freesurfer',0);


% --- Executes during object creation, after setting all properties.
function checkbox4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
setappdata(0,'dr_cov','');


% --- Executes during object creation, after setting all properties.
function checkbox5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% setappdata(0,'mb_ssim',0);


% --- Executes during object creation, after setting all properties.
function checkbox6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% setappdata(0,'mb_pca',0);


% --- Executes during object creation, after setting all properties.
function uibuttongroup3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uibuttongroup3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
mb_algorithm = 'euclidean';
setappdata(0,'mb_algorithm',mb_algorithm);


% --- Executes when selected object is changed in uibuttongroup3.
function uibuttongroup3_SelectionChangedFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uibuttongroup3 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject,'string');
switch str
    case 'euclidean'
        mb_algorithm = 'euclidean';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'seuclidean'
        mb_algorithm = 'seuclidean';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'cityblock'
        mb_algorithm = 'cityblock';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'minkowski'
        mb_algorithm = 'minkowski';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'chebychev'
        mb_algorithm = 'chebychev';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'mahalanobis'
        mb_algorithm = 'mahalanobis';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'cosine'
        mb_algorithm = 'cosine';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'correlation'
        mb_algorithm = 'correlation';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'spearman'
        mb_algorithm = 'spearman';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'hamming'
        mb_algorithm = 'hamming';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
    case 'jaccard'
        mb_algorithm = 'jaccard';
%         fprintf('Algorithm is : %s \n',mb_algorithm);
end
setappdata(0,'mb_algorithm',mb_algorithm);


% --- Executes during object creation, after setting all properties.
function checkbox14_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function checkbox13_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function checkbox10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called



% --- Executes during object creation, after setting all properties.
function checkbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on button press in checkbox15.
function checkbox15_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if get(handles.checkbox15,'value')
%     setappdata(0,'dr_mat',1); % 1- read MAT data
    set(handles.checkbox3,  'Enable','off');
    set(handles.checkbox2,  'Enable','off');
    set(handles.popupmenu1, 'Enable','off');
    set(handles.popupmenu2, 'Enable','off');
    set(handles.popupmenu3, 'Enable','off');
else
%     setappdata(0,'dr_mat',0); % 0- do not read MAT data
    set(handles.checkbox3,  'Enable','on');
    set(handles.checkbox2,  'Enable','on');
    set(handles.popupmenu1, 'Enable','on');
    set(handles.popupmenu2, 'Enable','on');
    set(handles.popupmenu3, 'Enable','on');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox15


% --- Executes during object creation, after setting all properties.
function checkbox15_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% setappdata(0,'dr_mat',0); % 1- read MAT data


% --- Executes during object creation, after setting all properties.
function checkbox8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to checkbox8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox 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


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
folder_path = uigetdir(pwd);
set(handles.edit8,'string',folder_path);
clear folder_name


function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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


% --- Executes on button press in checkbox16.
function checkbox16_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox16


% --- Executes on button press in checkbox17.
function checkbox17_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox17 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox17
if get(handles.checkbox17,'value')
    folder_path = uigetdir(pwd,'Select folder for saving matrix');
    set(handles.edit9,'string',folder_path);
else
    set(handles.edit9,'string',[]);
end


function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (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


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over popupmenu1.
function popupmenu1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in checkbox18.
function checkbox18_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox18 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox18
if get(handles.checkbox18,'value')
    folder_path = uigetdir(pwd,'Select folder for saving matrix');
    set(handles.edit10,'string',folder_path);
else
    set(handles.edit10,'string',[]);
end


function edit10_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double


% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (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


% --- Executes on button press in checkbox19.
function checkbox19_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox19 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox19
