
%function varargout = GMM(varargin)
function GMM(varargin)

% brief description of function GMM

% default input parameters
%tw='0.00666 0.01171 0.01221 0.01269 0.01474 0.01526 0.01546 0.01641 0.01760 0.01798 0.01876 0.01916 0.02639 0.02787 0.02952 0.03005 0.03040 0.03544 0.03598 0.03690 0.03756 0.03852 0.03954 0.04103 0.04203 0.04293 0.04361 0.04628 0.04888 0.05399 0.06157 0.07288';
%tm='1.08882 -0.95840 -1.70608 -4.26340 -0.57229 0.43587 -1.62997 -1.02968 -1.66160 -0.34715 -0.94276 0.00240 -0.30628 -0.02178 0.21902 0.79610 0.41616 0.87066 0.77253;1.71835 -2.83151 0.00281 -3.48369 -0.51925 -0.67968 -2.19372 -0.34728 -0.67818 0.04545 -0.66872 -0.61299 -0.32086 -0.86374 -0.36946 0.12720 -0.01428 0.48665 0.38636;-3.96737 1.78135 -1.80441 -1.23837 -0.76861 -1.66957 -0.60987 -0.91124 -0.11876 -0.65125 -0.47565 -0.07982 -0.28036 -0.28092 -0.02180 0.00539 0.02477 0.13392 0.13322;0.17922 -0.80113 -1.11853 -1.59671 -2.56289 -0.89715 -0.40240 -0.26244 -0.44359 0.08662 -0.06867 0.33426 0.53230 0.28564 -0.13978 0.10309 -0.39294 -0.28761 -0.12973;-0.20150 -1.74398 -1.29951 -2.08916 -1.34409 -1.35392 -1.60681 -0.18674 -0.24149 -0.41134 -0.55755 -0.13171 -0.24082 -0.46562 -0.34807 0.13524 -0.13647 0.47030 0.06474;-2.15841 -2.70598 0.63606 -2.29202 -1.00947 -0.40056 -0.23686 -0.70519 -1.13160 -0.52631 -0.26033 -0.32864 -0.32628 -0.34844 0.03474 0.00530 0.18897 0.37162 0.03302;4.15195 0.52494 -1.57766 -3.07428 -2.68847 -0.13967 -0.58553 -0.17348 -0.75802 -0.14019 -0.37179 0.11798 -0.27696 -0.44588 -0.55835 -0.59200 -0.33508 -0.16026 0.00722;0.56490 -2.73793 -0.40918 -1.99409 -2.89873 -0.76201 -0.82371 -0.05021 -1.30680 -0.64537 -1.06045 -0.11014 -0.02513 0.17189 0.54576 1.07748 0.70153 1.14321 -0.09243;0.98020 -1.96997 -0.92996 -1.85572 -2.82091 -0.43338 -0.87789 -0.06728 -0.66689 -0.27746 -1.00371 -0.14524 -0.47596 0.10997 -0.54283 -0.26097 -0.48052 0.25274 0.20901;-0.07475 -4.51095 -1.34560 -2.09629 -1.24963 0.43580 0.22133 -0.21223 -1.07398 -1.04449 0.03090 -0.19681 0.20107 -0.44075 0.38990 0.81736 0.65319 0.98469 0.42361;4.76599 -3.35426 -4.47051 -0.26876 -0.86497 -1.09282 -1.94613 1.26616 -0.47629 -0.23899 -0.34902 -0.14854 -0.31817 -0.31649 -0.12363 -0.54240 -0.23548 -0.29742 0.06960;0.15316 -1.66510 1.50662 -2.32040 -2.79096 -1.26720 -0.98277 -0.34537 -1.06085 -0.02404 -0.07433 -0.19046 -0.21151 -0.51736 -0.46642 -0.60040 -0.77586 -0.02667 -0.34639;0.11311 -2.73197 1.46933 -3.35660 -2.38748 -0.96137 -0.48337 0.12506 -0.82536 0.40686 -0.24966 -0.46913 -0.56360 -0.88374 -0.47063 -0.32937 -0.03548 0.52908 0.34116;-2.13395 -2.28877 2.34569 -3.44085 -1.87013 -0.86625 -1.18344 -0.77730 -1.58460 -0.00987 -0.89258 -0.66367 -0.00158 -0.10780 0.53374 0.61042 0.67491 0.67284 0.33736;3.08447 -0.70067 -2.18649 -4.93229 -0.66567 0.81073 -1.35411 0.55289 -0.96956 -0.30485 -0.08203 0.11901 -0.51495 -0.51889 -0.38684 -0.59261 -0.19614 0.09781 -0.06483;-1.21423 -0.78896 2.50743 -3.63404 -2.42969 -0.47950 -1.05269 -0.77414 -0.88997 -0.06157 -0.77111 -0.79424 -0.23247 -0.95505 -0.68241 -0.34412 -0.23871 0.02821 -0.06546;1.21578 1.41364 0.32507 -0.65499 -1.37479 -1.39169 -1.22958 -0.84859 -0.81207 -0.71043 -0.78142 -0.71255 -0.75078 -0.75917 -0.51089 -0.31960 -0.31460 -0.19130 -0.02899;2.43865 -1.59061 -1.66246 -2.03316 -1.02367 -0.81364 -1.65916 -0.50083 -0.44349 -0.31468 -0.14488 -0.08418 -0.45801 -0.32892 -0.67640 -0.63116 -0.63613 -0.18010 -0.02544;2.20522 -0.53300 -1.08987 -1.53208 -3.87276 -0.11039 -0.14888 -0.63516 -1.10287 -0.43787 -0.97787 0.07443 -0.54962 -0.58728 -0.91002 -0.14552 -0.68020 -0.16433 -0.04895;2.49489 -4.58443 -2.19983 -3.57749 0.61961 0.73360 -0.86691 -0.42848 -0.23245 0.12664 -0.42788 -0.20951 -0.34381 -0.18855 -0.23245 -0.17811 -0.32881 -0.00786 0.16512;-4.00999 -0.20688 -0.36600 0.04932 0.04092 0.17252 0.12277 0.25488 0.13500 0.12034 0.06158 0.14083 0.01306 0.06321 0.04840 0.00277 0.08142 0.06254 0.06347;3.73551 -2.54496 -2.07566 -3.66979 -1.36964 0.77798 -0.76781 -0.48799 -0.70817 0.25810 -0.42763 0.30403 -0.53498 -0.40291 -0.21266 -0.33791 -0.10279 0.01474 0.09231;-2.43882 -0.59396 0.20469 -1.10416 -0.90516 -0.64478 -0.43729 -0.31418 -0.33907 -0.31466 -0.32077 -0.15908 -0.18710 -0.29317 -0.17525 -0.08239 -0.08119 0.01753 0.10242;0.89700 -3.19373 -0.12486 -2.41729 -1.84551 -1.24695 -1.08548 0.49690 -0.58050 -0.42837 -0.25167 0.09810 -0.34793 -0.66896 -0.49066 -0.11344 -0.71425 0.01436 -0.23037;2.21936 -5.22220 -1.78685 -2.47915 -0.51702 -0.27507 -0.53228 0.26501 -0.43866 -0.11127 -0.82057 -0.18839 -0.12426 -0.02248 -0.45469 -0.22273 -0.04648 0.09178 0.17010;0.70317 -5.15764 -1.57749 -2.03407 -0.15771 -0.58317 -0.24246 -0.25695 0.03119 -0.76596 -0.07452 -0.03159 -0.24376 -0.45066 -0.42012 -0.05761 -0.13652 -0.26010 0.04850;-8.48002 -0.52916 0.94904 -1.64795 -0.67550 0.17496 -0.13732 -0.11033 0.03792 -0.30423 0.10250 0.17365 -0.15765 -0.21834 0.11587 0.07661 -0.12491 -0.13214 0.15663;3.41563 -4.59638 -2.00003 -0.84294 -1.56449 -1.44834 -1.30874 1.18067 -0.84303 -0.27582 -0.13277 -0.04530 -0.67039 -0.29628 -0.35461 -0.33697 -0.06515 0.20595 0.09866;-2.19189 -2.20465 -1.50810 -1.14942 -0.08147 -0.56425 -0.23140 0.11840 -0.01451 0.11790 -0.02036 -0.00169 -0.16887 -0.19851 -0.03240 0.00026 0.04896 0.08747 -0.00474;1.00883 -4.41592 -0.85867 -2.52663 -0.81016 -0.82368 -1.24515 -0.21903 -0.86236 -0.41664 -0.75355 0.07054 -0.20741 0.04103 0.48776 0.84993 0.60222 0.63699 0.26001;-4.83289 -1.68914 -1.25734 -0.91016 -0.37651 -0.41433 -0.13001 -0.03642 -0.03005 -0.10008 -0.04279 0.03086 -0.12949 -0.06781 -0.00604 -0.03228 -0.01680 -0.00478 0.01642;-8.27990 1.62933 -1.97884 -0.43665 0.33717 -0.83543 0.59011 -0.15298 0.04664 -0.07259 -0.03302 0.17413 -0.16801 -0.00546 -0.05652 0.04099 0.03290 -0.00199 0.01048';
%tv='0.67053 2.01773 0.44261 0.18652 0.04130 0.05672 0.04441 0.10539 0.30755 0.09337 0.04075 0.10152 0.05951 0.05059 0.05160 0.26875 0.05191 0.04033 0.01473;1.46819 0.70189 0.58631 0.33494 0.23753 0.37194 0.08932 0.66278 0.54660 0.14041 0.11226 0.19238 0.14294 0.19192 0.09307 0.19839 0.07776 0.08428 0.05108;4.66251 2.44234 0.51628 0.51859 0.40873 0.38120 0.22788 0.07443 0.26911 0.19801 0.08053 0.24247 0.10070 0.23871 0.37911 0.29490 0.16243 0.07791 0.03771;2.00551 1.21081 0.36352 0.75343 0.69645 0.27960 0.33439 0.34371 0.36129 0.27128 0.26121 0.49264 0.40937 0.26304 0.25400 0.21162 0.17019 0.10952 0.02802;1.29969 0.39433 0.44173 0.41016 0.23856 0.12528 0.13686 0.25340 0.43204 0.08437 0.13816 0.14641 0.22632 0.26522 0.20778 0.10581 0.09520 0.17805 0.09038;2.26900 0.71057 0.52997 0.30969 0.34757 0.30138 0.43946 0.13247 0.39958 0.13486 0.09065 0.05157 0.19630 0.08497 0.08485 0.07804 0.08418 0.06408 0.05665;0.17754 0.63225 0.18479 0.42149 0.44969 0.41234 0.11113 0.17375 0.23533 0.22692 0.08339 0.10226 0.08093 0.12109 0.11211 0.04477 0.05223 0.15576 0.21652;1.41801 0.24361 0.24885 0.16116 0.22517 0.14535 0.07378 0.13107 0.06488 0.15899 0.20509 0.12090 0.10499 0.10509 0.46341 0.44915 0.18013 0.11905 0.19117;0.81675 0.33831 0.31878 0.23998 0.15498 0.34011 0.17777 0.09092 0.09798 0.14698 0.14731 0.28610 0.27473 0.16626 0.11313 0.05507 0.08469 0.28514 0.16908;0.41056 0.51766 0.36472 0.21555 0.52581 0.20532 0.12588 0.10948 0.10572 0.10820 0.05989 0.04543 0.02305 0.01586 0.07158 0.28188 0.08648 0.09825 0.17810;0.35574 0.37846 0.54727 0.21315 0.18447 0.36579 0.21246 0.17844 0.26925 0.13992 0.08731 0.09915 0.03157 0.04975 0.17206 0.14281 0.09639 0.12985 0.06539;0.86858 1.28856 0.27539 0.51832 0.33372 0.17914 0.10862 0.19798 0.08468 0.10207 0.05239 0.10058 0.13218 0.10319 0.08015 0.07369 0.03605 0.05591 0.04230;0.52997 0.66131 0.55983 0.30355 0.24945 0.20754 0.12082 0.19100 0.20789 0.13849 0.15925 0.19861 0.20127 0.09535 0.08064 0.04131 0.07676 0.14986 0.07645;0.72308 1.06073 0.58063 0.29472 0.19160 0.10581 0.14374 0.24827 0.12577 0.21069 0.12063 0.09737 0.14027 0.20155 0.41068 0.25635 0.08314 0.03782 0.08321;0.58681 1.50372 0.28548 0.38333 0.45167 0.32281 0.21857 0.26581 0.17591 0.25498 0.12910 0.09559 0.25067 0.16102 0.04046 0.07139 0.08971 0.08715 0.06079;0.61884 0.64936 0.22049 0.97149 0.24416 0.13793 0.10228 0.15465 0.22735 0.14794 0.16483 0.09203 0.09415 0.11495 0.03928 0.07322 0.11444 0.10268 0.06542;1.36904 0.87443 0.38640 0.41041 0.54647 0.27095 0.23948 0.26958 0.18347 0.15236 0.14994 0.14356 0.13887 0.09360 0.10540 0.14850 0.12741 0.10390 0.07273;2.21360 1.80845 1.51694 1.02208 0.42580 0.41989 0.21652 0.19370 0.37643 0.21962 0.26413 0.30299 0.17969 0.12473 0.06382 0.08242 0.08295 0.04560 0.04156;0.30319 0.62106 0.14207 0.19070 0.19925 0.26465 0.13623 0.22837 0.14178 0.14426 0.12846 0.17950 0.14662 0.09167 0.06251 0.08320 0.04457 0.14578 0.04570;0.27084 0.51708 0.10889 0.96917 0.30358 0.35329 0.31648 0.18558 0.13663 0.13075 0.11331 0.05101 0.13885 0.09601 0.03762 0.08835 0.11482 0.11285 0.10041;0.21858 0.29333 0.10568 0.19146 0.13688 0.16707 0.09545 0.10970 0.10113 0.09770 0.10588 0.10093 0.09127 0.08246 0.05836 0.05760 0.06313 0.04200 0.03853;0.38325 0.93000 0.36620 0.56684 0.38670 0.27346 0.25385 0.23687 0.17094 0.19960 0.11762 0.19178 0.31430 0.04301 0.09329 0.14493 0.14993 0.24638 0.12702;4.65802 1.61045 1.18744 1.32434 0.78318 0.46068 0.40690 0.43278 0.22911 0.22795 0.13165 0.10403 0.07405 0.09397 0.10228 0.09372 0.05333 0.06778 0.05784;0.70051 1.68131 0.33222 0.25573 0.45104 0.32574 0.65925 0.18138 0.20557 0.14223 0.10922 0.24702 0.16351 0.08701 0.07190 0.05304 0.11451 0.05635 0.07123;0.69756 1.03820 0.69793 0.54223 0.56740 0.26661 0.18024 0.37984 0.35375 0.30835 0.07959 0.19317 0.09367 0.12702 0.10308 0.09758 0.11686 0.15243 0.17272;0.70674 0.43203 0.40162 0.17250 0.72302 0.43102 0.54002 0.27653 0.16129 0.19057 0.07018 0.17545 0.18050 0.07789 0.13531 0.09655 0.18275 0.05511 0.05326;0.93232 0.32546 0.36582 0.19755 0.26306 0.20214 0.16081 0.15379 0.15760 0.09880 0.11017 0.08537 0.07823 0.06146 0.05863 0.06024 0.04969 0.04619 0.05245;0.69738 0.91943 1.91642 0.67397 0.77041 0.31274 0.38264 0.31662 0.29520 0.28711 0.21585 0.13688 0.22846 0.15289 0.07561 0.08697 0.12134 0.21672 0.20153;6.86372 2.91260 1.57812 0.95797 0.99399 0.66281 0.50425 0.33105 0.28206 0.18453 0.16277 0.15113 0.12615 0.10503 0.08996 0.09430 0.06295 0.03849 0.04444;1.50483 2.16002 1.61701 0.96196 1.05925 0.69380 0.53859 0.73142 0.29285 0.32360 0.16598 0.26739 0.25539 0.13492 0.20052 0.25158 0.10392 0.09590 0.24219;3.02541 1.48131 0.46534 0.37010 0.23598 0.15224 0.10108 0.11612 0.13900 0.08361 0.08030 0.06616 0.07516 0.05439 0.04918 0.04113 0.04259 0.05481 0.03414;0.57079 0.72530 0.34603 0.45938 0.26690 0.19706 0.10698 0.14653 0.09510 0.07206 0.09858 0.06881 0.06788 0.05907 0.05616 0.06072 0.04071 0.03397 0.03252';
tw = '0.3 0.2 0.5';
tm = '1.08 -0.95 -1.70 -4.26; -0.57 0.43 -1.62 -1.02; -1.66 -0.34 -0.94 0.11';
tv = '0.67 0.01 0.44 0.18; 0.04 0.05 0.04 0.10; 0.30 0.09 0.04 0.10';
                   
%-------------------- Declare non-ui data ---------------------------------

    nDim = 0;  % number of dimensions
    nMix = 0;  % number of mixtures
    checkboxDim = []; % storing the handles of all checkboxes for the selection of dimensions 
    checkboxMix = []; % storing the handles of all checkboxes for the selection of mixtures 
    dimChecked = []; % storing the handles of checkboxes for dimensions selected
    mixChecked = []; % storing the handles of checkboxes for mixtures selected
    mixLine = {}; % storing the handles of the lines which draw checked mixtures 
    cMenu = [];  % storing the handles of context menu for mixture lines
    jointDensity = 0; 
    sumDimChecked = 0; % number of dimensions which are picked
    gridCol = 0;
    gridRow = 0;
    heights = []; % heights of mixtures which are picked
    
% ------------------  Construct components --------------------------------

    % set figure invisible during construction
    defaultBackground = [232/255, 236/255, 246/255];
    
    fg = figure('Visible','off','Name','GMM',...
               'Units','pixels',...
               'Menubar','None',...
               'Toolbar','figure',...
               'Color', defaultBackground,...
               'Position',[20, 100, 820, 760]); 
    pa = uipanel('Parent',fg,... % panel for plot
         'Units','pixels',...
         'Position',[20, 130, 600, 600]);
    panel_dim_copy = uipanel('Parent',fg,... % panel for clearing all child-objects of panel_dim 
                     'BorderType','none',... 
                     'Units','pixels',...
                     'Position',[630, 480, 180, 250]);     
    panel_dim = uipanel('Parent',fg,... % panel for checkboxes and radiobuttons for selecting dimensions
                     'Title','Dimensions',...
                     'Units','pixels',...
                     'Position',[630, 480, 180, 250]);     
    panel_mix_copy = uipanel('Parent',fg,... %panel for clearing all child-objects of panel_mix 
                     'BorderType','none',... 
                     'Units','pixels',...
                     'Position',[630, 220, 180, 250]);             
    panel_mix = uipanel('Parent',fg,... % panel for checkboxes and radiobuttons for selecting mixtures
                     'Title','Mixtures',...
                     'Units','pixels',...
                     'Position',[630, 220, 180, 250]);             
    % fields/buttons for entering/selecting parameters
    pushbutton_draw2D = uicontrol(fg,'Style','pushbutton',...
                    'String','Draw 2D',...
                    'Units','pixels',...
                    'Position',[630 160 70 30],...
                    'Callback',{@pushbutton_draw2D_Callback});
    pushbutton_draw3D = uicontrol(fg,'Style','pushbutton',...
                    'String','Draw 3D',...
                    'Units','pixels',...
                    'Position',[720 160 80 30],...
                    'Callback',{@pushbutton_draw3D_Callback},...
                    'enable','off');
    saticText_jointDensity = uicontrol(fg,'Style','text',...
                    'String','Joint Density',...
                    'BackgroundColor',defaultBackground,...
                    'Units','pixels',...
                    'Position',[625 120 70 14]);                
    buttongroup_jointDensity = uibuttongroup(fg,... % buttongroup for seleting the value of joint density
                    'BackgroundColor',defaultBackground,...
                    'BorderType','none',... 
                    'Units','pixels',...
                    'Position',[700, 122, 70, 20],...
                    'SelectionChangeFcn', {@buttongroup_jointDensity_Callback});            
    radiobutton_jointDensity_0 = uicontrol(buttongroup_jointDensity,...
                    'Style','radiobutton',...
                    'String','0',...
                    'BackgroundColor',defaultBackground,...
                    'Value', 0,...
                    'Units','pixels',...
                    'Position',[0, 0, 40, 14]);
    radiobutton_jointDensity_1 = uicontrol(buttongroup_jointDensity,...
                    'Style','radiobutton',...
                    'String','1',...
                    'BackgroundColor',defaultBackground,...
                    'Value', 0,...
                    'Units', 'pixels',...
                    'Position',[30, 0, 40, 14]);
    radiobutton_jointDensity_01 = uicontrol(buttongroup_jointDensity,...
                    'Style','radiobutton',...
                    'String','0 && 1',...
                    'BackgroundColor',defaultBackground,...
                    'Value', 0,...
                    'Units', 'pixels',...
                    'Position',[60, 0, 50, 14]);
    saticText_23d = uicontrol(fg,'Style','text',...
                    'String','low',...
                    'BackgroundColor',defaultBackground,...
                    'Units','pixels',...
                    'Position',[81 100 30 14]);   
    saticText_2d = uicontrol(fg,'Style','text',...
                    'String','high',...
                    'BackgroundColor',defaultBackground,...
                    'Units','pixels',...
                    'Position',[155 100 20 14]);
    saticText_resolution = uicontrol(fg,'Style','text',...
                    'String','Resolution',...
                    'BackgroundColor',defaultBackground,...
                    'Units','pixels',...
                    'Position',[10 85 70 14]); 
    slider_resolution = uicontrol(fg,'Style','slider',...
                    'Max', 2000,...
                    'Min', 10,...
                    'Value',300,...
                    'SliderStep',[0.05 0.2],...
                    'Position',[80 85 100 14],...
                    'Callback',{@slider_resolution_Callback});
    staticText_scale = uicontrol(fg,'Style','text',...
                    'String','Scale',...
                    'BackgroundColor',defaultBackground,...
                    'Units','pixels',...
                    'Position',[360 85 50 14]); 
    pushbutton_addRatio = uicontrol(fg,'Style','pushbutton',...
                    'String','+',...
                    'Units','pixels',...
                    'Position',[425 85 30 25],...
                    'Callback',{@pushbutton_addRatio_Callback});  
    pushbutton_subRatio = uicontrol(fg,'Style','pushbutton',...
                    'String','-',...
                    'Units','pixels',...
                    'Position',[455 85 30 25],...
                    'Callback',{@pushbutton_subRatio_Callback});  
    togglebutton_logRatio = uicontrol(fg,'Style','togglebutton',...
                    'String','log  ||  linear',...
                    'Units','pixels',...
                    'Position',[510 85 65 25],...
                    'Callback',{@togglebutton_logRatio_Callback}); 
    saticText_weight = uicontrol(fg,'Style','text',...
                    'String','Weight',...
                    'BackgroundColor', defaultBackground,...
                    'Units','pixels',...
                    'Position',[20 62 50 14]);         
    editText_weight = uicontrol(fg,'Style','edit',...
                    'String', tw,...
                    'Units','pixels',...
                    'Position',[70 62 740 14],...
                    'Callback',{@editText_weight_Callback});
    saticText_mean = uicontrol(fg,'Style','text',...
                    'String','Mean',...
                    'BackgroundColor',defaultBackground,...
                    'Units','pixels',...
                    'Position',[20 46 50 14]);
    editText_mean = uicontrol(fg,'Style','edit',...
                    'String',tm,...
                    'Position',[70 46 740 14],...
                    'Callback',{@editText_mean_Callback});
    saticText_variance = uicontrol(fg,'Style','text',...
                    'String','Variance',...
                    'BackgroundColor',defaultBackground,...
                    'Units','pixels',...
                    'Position',[20 30 50 14]); 
    editText_variance = uicontrol(fg,'Style','edit',...
                    'String',tv,...
                    'Position',[70 30 740 14],...
                    'Callback',{@editText_variance_Callback});    

   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   % to add the input-field for zero-value into GUI:
   % staticText_zero = uicontrol(fg,'Style','text',...
   %                 'String','zero',...
   %                 'BackgroundColor',defaultBackground,...
   %                 'Units','pixels',...
   %                 'Position',[185 100 70 14]); 
   % editText_zero = uicontrol(fg,'Style','edit',...
   %                  'String', 0.0001,...
   %                 'Units','pixels',...
   %                 'Position',[260 100 100 14],...
   %                 'Callback',{@editText_zero_Callback}); 
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % generate checkboxes and radio buttons for the selection of dimensions and mixtures  
    
    % declare the handles of gui-components in the outermost function
    panel_radioDim = 1;  % handle of the panel for two radio buttons for selecting all/none of the dimensions
    panel_radioMix = 1;  
    radioButton_dimNone = 1; %handle of the radio button for selecting none of the dimensions 
    radioButton_dimAll = 1;   
    radioButton_mixNone = 1;      
    radioButton_mixAll = 1;
    
    function  checkRadio_dm_Create(nDim, nMix) 
        % 10 checkboxes in a column, the margin between the last checkbox and the bottom is not less than 40 pixels, for 
        % the height of panel for the radio buttons is 35 pixels.
        pos1 = [3, 230, 33, 15];    
        for t1 = 1:nDim,        
            if ( (mod(t1,10) == 1 && t1 ~= 1) || pos1(1,2) < 40 ) 
                pos1(1,1) = pos1(1,1) + 35;
                pos1(1,2) = 230;
            end        
            pos1(1,2) = pos1(1,2)- 18; % margin to bottom is then 18 pixels less.
            % generate checkboxes for the selection of dimensions
            %%%% 'Tag' instead of 'global' for uicontrol elements doesn't work here
            checkboxDim(t1) = uicontrol('Parent', panel_dim_copy,... % checkboxDim(t1) stands for checkbox dim t1-1
            'Style', 'checkbox',...
            'String', int2str(t1 - 1),...
            'Value', 1,...
            'Units', 'pixels',...  %%%% define 'units' before 'position'
            'Position', pos1,...
            'Callback', {@checkbox_dim_Callback});   
            set(checkboxDim(t1),'Units','normalized') % change 'units' to 'normalized' so that checkboxes resize automatically
        end

        % generate radio buttons and a panel for them
        panelRadio_dim = uipanel('Parent',panel_dim_copy,... % panel for radio buttons  
        'BorderType','none',...
        'Units', 'pixels',...
        'Position',[3, 3, 170, 35]);

        radioButton_dimNone = uicontrol('parent',panelRadio_dim,...  % radio button 'Select None' 
        'Tag', 'radioButton_dimNone',...
        'Style', 'radiobutton',...
        'String', 'Select None',...
        'Value', 0,...
        'Units', 'pixels',...
        'Position',[3, 20, 160, 15],...
        'Callback',@radioButton_dimNone_Callback);

        radioButton_dimAll = uicontrol('parent',panelRadio_dim,...  % radio button 'Select All' 
        'Tag','radioButton_dimAll',...
        'Style', 'radiobutton',...
        'String', 'Select All',...
        'Value', 0,...
        'Units', 'pixels',...
        'Position', [3, 3, 160,15],...
        'Callback',@radioButton_dimAll_Callback);


        pos2 = [3, 230, 33, 15];  
        for t2 = 1:nMix,  
            if ( (mod(t2,10) == 1 && t2 ~= 1) || pos2(1,2) < 40 ) 
                pos2(1,1) = pos2(1,1) + 35;
                pos2(1,2) = 230;
            end      
            pos2(1,2) = pos2(1,2)- 18;

            checkboxMix(t2) = uicontrol('Parent', panel_mix_copy,...  
            'Tag',  strcat('mix', int2str(t2 - 1)),...  % join two strings to make a single string as a checkbox-tag 
            'Style', 'checkbox',...
            'String', int2str(t2 - 1),...
            'Value', 1,...
            'Units', 'pixels',...
            'Position', pos2,...
            'Callback',{@checkbox_mix_Callback});
            set(checkboxMix(t2),'Units','normalized');    
        end

        panelRadio_mix = uipanel('Parent', panel_mix_copy,... % panel for radio buttons
        'BorderType','none',...
        'Units', 'pixels',...
        'Position',[3, 3, 170,35]);

        radioButton_mixNone = uicontrol('parent', panelRadio_mix,...  % radio button 'Select None' 
        'Tag','radioButton_mixNone',...
        'Style', 'radiobutton',...
        'String', 'Select None',...
        'Value', 0,...
        'Units', 'pixels',...
        'Position',[3, 20, 160,15],...
        'Callback',@radioButton_mixNone_Callback);

        radioButton_mixAll = uicontrol('parent', panelRadio_mix,...  % radio button 'Select All' 
        'Tag','radioButton_mixAll',...
        'Style', 'radiobutton',...
        'String', 'Select All',...
        'Value', 0,...
        'Units', 'pixels',...
        'Position', [3, 3, 160,15],...
        'Callback', @radioButton_mixAll_Callback);

        set([panelRadio_dim, panelRadio_mix,...
        radioButton_dimNone,radioButton_dimAll,...
        radioButton_mixNone, radioButton_mixAll],...      
        'Units','normalized'); 
    
    end 

    % define context menu to show/clear the legend for all mixtures
    cp = uicontextmenu; % contextmenu 
    item1_cp = uimenu(cp,'Label', 'Legend ON', 'Callback', {@legend_On_Callback});  % menu item 
    item2_cp = uimenu(cp,'Label', 'Legend OFF', 'Callback', {@legend_Off_Callback}); 
    set(pa, 'uicontextmenu', cp); 
    

%-------------------  Initialize GUI -------------------------------------

    % move the GUI to the center of the screen
    movegui(fg,'center'); 
    % make the GUI visible
    set(fg,'Visible','on');

    % change units to 'normalized' so that components resize automatically
    % to add fields into GUI for zero-input:  staticText_zero, editText_zero  
    set([fg,pa,panel_dim,panel_mix, panel_dim_copy,panel_mix_copy,saticText_weight,editText_weight,...
        saticText_mean, editText_mean, saticText_variance,editText_variance,...
        editText_variance,saticText_jointDensity, buttongroup_jointDensity,...
        radiobutton_jointDensity_0,radiobutton_jointDensity_1, radiobutton_jointDensity_01,...
        saticText_23d, saticText_2d, saticText_resolution, slider_resolution, staticText_scale... 
        pushbutton_addRatio, pushbutton_subRatio,togglebutton_logRatio,...
        pushbutton_draw2D,pushbutton_draw3D],...
        'Units','normalized');  
   

%-------------- Callback-functions on press of buttons or input in fields ---------

    function editText_resolution_Callback(hObject, eventdata)
        guidata(hObject);
        check_resolution;
    end

    function slider_resolution_Callback(hObject, eventdata, handles)
        slider_value = get(hObject,'Value');
        check_resolution;
    end

    function r = check_resolution
        r = get(slider_resolution,'value');
    end  

    function editText_zero_Callback(hObject, eventdata)
        guidata(hObject);
        check_zero;
    end

    function z = check_zero
        z = 0.0001; % default minimum value of y in 2d-plot
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %% 
      %  entry_zero = get(editText_zero,'string');
      %  [z, sz]= str2num(entry_zero);
      %  if (~ sz)
      %     z = 0.0001; % default minimum value of y(2d) or z (3d)
      %  end
      %  v = check_variance;
      %  d = length(v(1, :)); %columns of the variance matrix are feature dimensions
      %  M = length(v(:, 1)); %rows of the variance matrix are mixture components
      %  for m = 1:M      
      %      if(log(z)+0.5*log((2*pi)^d*prod(v(m,:)))>= 0)
      %          z1 = 1/sqrt((2*pi)^d*prod(v(m,:)));
      %          msg = strcat('The value of ''zero'' must be smaller than ', num2str(z1));
      %          msgbox(msg);
      %          return;
      %      end
      % end
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    end
        
    function togglebutton_logRatio_Callback(hObject, eventdata)
        if(isempty(allchild(pa)))% if there is no plot in the panel
            return;
        end
        button_state = get(hObject,'Value');
        a = axis;  % get the axis limits
        if button_state == get(hObject,'Max')% on click of button
            if(length(a)== 4)% 2d -plot
                for p = 1:sumDimChecked
                    set(subplot(gridRow, gridCol, p), 'YScale','log'); 
                end
            elseif(length(a)== 6) % 3d-plot
                m = check_mean;
                v = check_variance;
                w = check_weight;
                j = jointDensity;
                r = check_resolution;
                z = check_zero;
                delete(allchild(pa)); 
                plot3d(dimChecked, mixChecked, w, m, v, j, r, z, 1);
                %set(gca,'ZScale','log');
            end
        elseif(button_state == get(hObject,'Min'))
            if(length(a)== 4)% 2d -plot
                for p =1:sumDimChecked
                    set(subplot(gridRow, gridCol, p), 'YScale','linear');
                end
            elseif(length(a)== 6) % 3d-plot
                m = check_mean;
                v = check_variance;
                w = check_weight;
                j = jointDensity;
                r = check_resolution;
                z = check_zero;
                delete(allchild(pa)); 
                plot3d(dimChecked, mixChecked, w, m, v, j, r, z, 0);
                %set(gca,'ZScale','linear');
            end           
        end
    end

    function h = getHeights
       m = check_mean;
       v = check_variance;
       w = check_weight;
       a = axis;
       if(isempty(heights))% get the y-limit before scaling
           if(length(a)== 4)% 2d -plot
               heights(1) = a(4); % assign current y-limit-value to the first element in heights-array
           elseif(length(a)== 6)% 3d -plot
               heights(1) = a(6); % assign current z-limit-value to the first element in heights-array
           end
       end
       i = 2;
       for M = 1:nMix  % get heights of single mixtures
          if(mixChecked(M) == 1)
             heights(i) = 1 / sqrt((2*pi)^nDim*prod(v(M,:)))* w(M); %  assign heights of the mixtures in the plot to the array
             i = i +1;
          end
       end 
       h = sort(heights,'descend');%  sort heights of the mixtures in the plot in descending order
    end

    function pushbutton_addRatio_Callback(hObject, eventdata)
       if(isempty(allchild(pa)))% if there is no object in the plot-panel
            return;
       end
       h = getHeights;
       a = axis;  % get the axis limits
       for i = 1:length(h)
           k = mod(i,length(h))+1;
           if(length(a)== 4)% 2d-plot
              if(a(4) >= h(i) || i == length(h)) % if current y-limit is larger than or equal to the maximum height in the plot / all 
                                                 % of the mixtures have been scaled up
                  for p = 1:sumDimChecked
                    set(subplot(gridRow, gridCol, p),'YLim', [a(3) h(k)]); % then focus on the next underlying mixture / reset y-limit to the original value before scaling
                  end
                  break;
              end
           else % 3d-plot
               if(a(6) >= h(i) || i == length(h))
                  set(gca,'ZLim', [a(5) h(k)]);
                  break;
               end
           end
       end
    end

    function pushbutton_subRatio_Callback(hObject, eventdata)
       if(isempty(allchild(pa)))
            return;
       end       
       a = axis;  % get the aixs limits
       h = getHeights;
       for i = length(h):-1:1
           if(i ~= 1)
               k = i-1;
           else
               k = 1;
           end
           if(length(a)== 4)% 2d -plot
              if(a(4) <= h(i) || i == 1) % if the current y-limit is smaller than or equal to the minimum height of the partly concealed mixtures
                                         % /all of the mixtures have been scaled down
                 for p =1:sumDimChecked
                        set(subplot(gridRow, gridCol, p),'YLim', [a(3) h(k)]); % then reshow the next overlying mixture / reset y-limit to the original value
                 end
                 break;
              end
           else % 3d -plot
               if(a(6) <= h(i) || i == 1)
                  set(gca,'ZLim', [a(5) h(k)]);
                  break;
               end
           end
       end
    end

    function buttongroup_jointDensity_Callback(hObject, eventdata)
        switch get(eventdata.NewValue,'String') % get string of selected object
            case '0'
                jointDensity = 0;
            case '1'
                jointDensity = 1;
            case '0 && 1' 
                jointDensity = 2;
        end
    end

    function editText_weight_Callback(hObject, eventdata)
        guidata(hObject);
        check_weight;
    end

    function w = check_weight
        entry_weight = get(editText_weight,'string');
        if(isempty(entry_weight))
            msgbox('There is no entry in the ''weight'' field.'); 
            return;
        else
            [w, sw] = str2num(entry_weight);
            if(~ sw)
                msgbox('Conversion from the entry in ''weight'' field to vector fails. Default value ''1'' is used');
                w = 1.0; % default value of weight;
            elseif(~ isvector(w))
                msgbox('The entry in the ''weight'' field is not a vector. Default value ''1'' is used');
                w = 1.0;
            end
        end
    end

    function m = check_mean
        entry_mean = get(editText_mean,'string');
        if(isempty(entry_mean))
            msgbox('There is no entry in the ''mean'' field.'); 
            return;
        else
            [m, sm] = str2num(entry_mean);
            if(~ sm)
                msgbox('Conversion from the entry in ''mean'' field to matrix fails.');
                return;
            elseif(isempty(m))
                msgbox('The entry in the ''mean'' field is not a matrix.');
                return;
            end
        end
        nDim = length(m(1, :)); %columns of the mean matrix are feature dimensions
        nMix = length(m(:, 1)); %rows of the mean matrix are mixture components
    end

    % checkboxes and radio buttons for selecting dimensions and mixtures are (re)generated 
    % on entry in the text field 'mean'     
    function editText_mean_Callback(hObject, eventdata)
        delete(allchild(panel_dim_copy));
        delete(allchild(panel_mix_copy));
        guidata(hObject);
        m = check_mean;
        checkRadio_dm_Create(nDim, nMix);        
        for t1 = 1:nDim,
            dimChecked(t1) = 0;
            if(get(checkboxDim(t1),'Value') == 1),
                dimChecked(t1) = 1;
            end
        end
        if(sum(dimChecked) == 2)
            set(pushbutton_draw3D,'Enable','on');
        else
            set(pushbutton_draw3D,'Enable','off');
        end
    end

    function editText_variance_Callback(hObject, eventdata)
        guidata(hObject);
        check_variance;
    end

    function v = check_variance
        m = check_mean;
        entry_variance = get(editText_variance,'string');
        if(isempty(entry_variance))
            msgbox('There is no entry in the ''variance'' field.'); 
            return;
        else
            [v, sv] = str2num(entry_variance);
            if(~ sv)
                msgbox('Conversion from the entry in ''variance'' field to matrix fails.');
                return;
            elseif(isempty(v))
                msgbox('The entry in the ''variance'' field is not a matrix.');
                return;
            elseif(size(v) ~= size(m))
                msgbox('Dimensions of ''mean'' and ''variance'' do not correspond.');
                return;
            end
        end
    end

    function checkbox_dim_Callback(hObject,eventdata)
        if (get(hObject,'Value') == 1), % if one checkbox is picked
            set(radioButton_dimNone,'Value',0); 
        else % if one checkbox is unchecked
            set(radioButton_dimAll,'Value',0);        
        end
        for t1 = 1:nDim,
            dimChecked(t1) = 0;
            if(get(checkboxDim(t1),'Value') == 1),
                dimChecked(t1) = 1;
            end
        end
        if(sum(dimChecked) == 2)
            set(pushbutton_draw3D,'Enable','on');
        else
            set(pushbutton_draw3D,'Enable','off');
        end
    end

    function checkbox_mix_Callback(hObject,eventdata)
        if (get(hObject,'Value') == 1), % if one checkbox is picked      
            set(radioButton_mixNone,'Value',0);
        else % if one checkbox is unchecked
            set(radioButton_mixAll,'Value',0);
        end
    end

    function radioButton_dimNone_Callback(hObject,eventdata)
        if (get(hObject,'Value') == get(hObject,'Max')),
            set(radioButton_dimAll,'Value',0);
            set(pushbutton_draw3D,'Enable','off');
            for t = 1:nDim,
                set(checkboxDim(t),'Value',0);
            end
        else
            
            return;
        end
    end

    function radioButton_dimAll_Callback(hObject,eventdata)
        if (get(hObject,'Value') == get(hObject,'Max')),
            set(radioButton_dimNone,'Value',0);
            set(pushbutton_draw3D,'Enable','off');
            for t = 1:nDim,
                set(checkboxDim(t),'Value',1);
            end
        else
            return;
        end
    end

    function radioButton_mixNone_Callback(hObject,eventdata)
        if (get(hObject,'Value') == get(hObject,'Max')),
            set(radioButton_mixAll,'Value', 0);
            for t = 1:nMix,
                set(checkboxMix(t),'Value',0);
            end
        else
            return;
        end
    end

    function radioButton_mixAll_Callback(hObject,eventdata)
        if (get(hObject,'Value') == get(hObject,'Max')),
            set(radioButton_mixNone,'Value',0);
            for t = 1:nMix,
                set(checkboxMix(t),'Value',1);
            end
        else
            return;
        end
    end
   
    function legend_On_Callback(hObject, eventdata)
        if (allchild(pa))% if the plot panel is not empty
            legend('Location', 'NorthEast');
           % legend boxoff; % make background transparent
        end
    end

    function legend_Off_Callback(hObject, eventdata)
        if (allchild(pa))
            legend('off');
        end
    end
    
    % executes on button press on "draw (2D)"
    function getDimMixChecked()  % get dimensions and mixtures which are picked 
       for t1 = 1:nDim,
            dimChecked(t1) = 0;
            if(get(checkboxDim(t1),'Value') == 1)
                dimChecked(t1) = 1;
            end
        end
        for t2 = 1:nMix,
            mixChecked(t2) = 0;
            if(get(checkboxMix(t2),'Value') == 1)
                mixChecked(t2) = 1;
            end
        end
    
    end

    function pushbutton_draw2D_Callback(hObject, eventdata)
        
        delete(allchild(pa)); % remove all objects on plot panel 
        heights = []; % reset heights-array;
        
        j = jointDensity; 
        w = check_weight;
        m = check_mean;      
        v = check_variance;
        r = check_resolution;
        z = check_zero;
        
        if(isempty(allchild(panel_dim_copy)) && isempty(allchild(panel_mix_copy)));
            checkRadio_dm_Create(nDim, nMix); 
        end
        
        getDimMixChecked();        
        sumDimChecked = sum(dimChecked); 
        sumMixChecked = sum(mixChecked);
        if(sumDimChecked == 0 || sumMixChecked == 0)
            return;
        end
        
        gridCol = max(ceil(sqrt(sumDimChecked)), 1); 
        gridRow = ceil(sumDimChecked/gridCol);

        plot2d(dimChecked, mixChecked, gridCol, gridRow, w, m, v, j, r, z);
    
    end
    
    function pushbutton_draw3D_Callback(hObject, eventdata)

        delete(allchild(pa)); % remove all objects from the plot panel
        heights = []; % reset heights-array;
        j = jointDensity; 
        w = check_weight;
        m = check_mean;      
        v = check_variance;
        r = check_resolution;
        z = check_zero; 
        
        if(isempty(allchild(panel_dim_copy)) && isempty(allchild(panel_mix_copy)));
            checkRadio_dm_Create(nDim, nMix); 
        end
        
        getDimMixChecked();
        sumDimChecked = sum(dimChecked); 
        sumMixChecked = sum(mixChecked);
        if(sumDimChecked == 0 || sumMixChecked == 0)
            return;
        end
        
        plot3d(dimChecked, mixChecked, w, m, v, j, r, z, 0);
        
    end


% ----------------------------- Utility functions for GMM------------------

% plotGMM(weight, mean, variance, singleGaussians, resolution)
% 
% plots the values of a 1D normal distribution; if resolution is not given,
% the curve is sampled with 1000 points between +-3*variance, otherwise
% the number of points given is used; if jointDensity is given ==1,
% the joint densitiy is plotted instead of the single weighted gaussian
% mixtures
% copyright (c) 2008 by thilo stadelmann,
% stadelmann@informatik.uni-marburg.de


    function plot2d(dimChecked, mixChecked, gridCol, gridRow, weight, mean, variance, jointDensity, resolution, zero)

        %define all possibilities for line-drawing styles that plot()recognizes
        colors = {'b' 'r' 'g' 'c' 'm'  'y' 'k'};     
        lines = {'-' ':' '-.' '--'};

        lineWidth =' 2';
        if (jointDensity == 0) 
            plotJd0(0);
        elseif (jointDensity == 1) 
            plotJd1(1);
        elseif (jointDensity == 2) 
            plotJd0(0);
            hold on;
            plotJd1(1);
        end

        function plotJd0(jointDensity)
            %draw individual gaussians, one subplot per dimension
            i = 1;
            for d = 1:nDim,      
                if(dimChecked(d) == 1), % dimension d-1 is selected
                    subplot(gridRow, gridCol, i, 'Parent', pa);
                    hold on;
                    for m = 1:nMix,                       
                        cc = mod(m,length(colors)) + 1;% rotate colors
                        lc = mod(m,length(lines)) + 1; % rotate line styles                       
                        if(mixChecked(m) == 1), % mixture m-1 is selected                            
                           style = sprintf('%s%s', colors{cc}, lines{lc}); 
                           % storing the handles of lines in a cell array 
                           mixLine{m} = plotNormal1d(nDim, weight(m), mean(m, d), variance(m, d), variance(m, :), resolution, style, zero); 
                           % specify the text label used by the legend for all mixtures                 
                           set(mixLine{m}, 'DisplayName', num2str(m - 1)); 
                           % create context menu showing legend for single mixture line
                           cMenu(m) = uicontextmenu;   
                           uimenu(cMenu(m),'Label', strcat('mixture', int2str(m - 1)));  % context menu item 
                           uimenu(cMenu(m),'Label', strcat('mean', num2str(mean(m, d))));
                           uimenu(cMenu(m),'Label', strcat('variance', num2str(variance(m, d))));
                           set(mixLine{m}, 'uicontextmenu', cMenu(m)); % associate mixture line with context menu                    
                        end                        
                    end            
                    s = sprintf('dim %d', d-1);
                    title(s);
                    i = i + 1;  
                end
            end
        end
        
        function plotJd1(jointDensity)
            %draw complete GMM density per dimension selected
            i = 1;
            for d = 1:nDim,
                if(dimChecked(d) == 1),
                    subplot(gridRow, gridCol, i, 'Parent', pa);
                    mixLine{d} = plotGMM1d(nDim, mixChecked, weight, mean(:, d), variance(:, d), variance, resolution, zero);

                    %define context menu showing legend
                    set(mixLine{d}, 'DisplayName', 'joint density');
                    cMenu(d) = uicontextmenu;   
                    uimenu(cMenu(d),'Label', 'joint density');  % menu item 
                    set(mixLine{d}, 'uicontextmenu', cMenu(d)); 

                    s = sprintf('dim %d', d-1);
                    title(s);
                    i = i + 1; 
                end
             end
        end
        
    end


    function plot3d(dimChecked, mixChecked, weight, mean, variance, jointDensity, resolution, zero, ratio)
    
        if (sum(dimChecked) ~= 2)
            return; 
        end

        d1 = -1;
        d2 = -1;
        for d = 1:nDim    
            if(dimChecked(d) == 1)
                if(d1 == -1)
                    d1 = d;
                else
                    d2 = d;
                    break;
                end
            end   
        end

        subplot(1, 1, 1,'Parent', pa);
        
        if jointDensity == 0
            plotJd0(0);
        elseif jointDensity == 1
            plotJd1(1);
        else
            plotJd0(0);
            hold on;
            plotJd1(1);
        end
        
        function plotJd0(jointDensity)
            i = 1;
            for m = 1:nMix
                if(mixChecked(m) == 1)
                    mean2d = [mean(m, d1); mean(m, d2)]; 
                    variance2d = diag([variance(m, d1), variance(m, d2)], 0); 
                    %color = [randi(4096)/4096, randi(4096)/4096,randi(4096)/4096]; % define a color in RGB model at random
                    %style = strcat('''FaceColor''',color,',','''FaceLighting''', ',', '''gouraud'''); 
                    colors = {'b' 'g'  'r'  'y' 'c' 'm'  'k'};  
                    cc = mod(m,length(colors)) + 1;% rotate colors
                    mixSurface{m} = plotNormal2d(nDim, d1-1, d2-1, weight(m), mean2d, variance2d, variance(m, :), resolution, colors{cc}, zero, ratio);
                   
                    % specify the text label used by the legend for all mixtures  
                    lb = strcat(int2str(m - 1), mat2str(mean2d), mat2str(variance2d));
                    set(mixSurface{m}, 'DisplayName', lb);
                    %set(mixSurface{m}, 'DisplayName', num2str(m - 1)); 
                    % create context menu for mixture surface
                    cMenu(m) = uicontextmenu;   
                    uimenu(cMenu(m),'Label', strcat('mixture', int2str(m - 1)));  % context menu item 
                    uimenu(cMenu(m),'Label', strcat('mean', mat2str(mean2d)));
                    uimenu(cMenu(m),'Label', strcat('variance', mat2str(variance2d)));
                    set(mixSurface{m},'uicontextmenu', cMenu(m)); % associate mixture surface with context menu                     
                    i = i + 1;
                end
                hold on;
            end
        end
        
        function plotJd1(jointDensity)          
            for m = 1:nMix
                    means2d(:,:,m) = [mean(m, d1); mean(m, d2)];
                    variances2d(:,:,m) = diag([variance(m, d1), variance(m, d2)], 0); 
            end
            color = 'b'; % select one of the built-in colormaps of MATLAB
            mixSurface = plotGMM2d(nDim, mixChecked, d1-1, d2-1, weight, means2d, variances2d, variance, resolution, color, zero, ratio);
            grid on;
            lb = 'joint density';
            set(mixSurface, 'DisplayName', lb);
        end
    end    
    
end